itHyperoX Posted December 4, 2017 Share Posted December 4, 2017 (edited) Hi, i'm working on a team base script, i'm stuck, i don't know why is the script is just doing something with the last created stuff in the TeamTable. TeamTable = { {"Kings", 2048.7641601563, 1368.6136474609, 10.671875, 2045, 1368.6136474609, 10.671875, 0, 0, 0}, {"WatoLocoos", 2066.482421875, 1369.6892089844, 10.671875, 2066, 1369.6892089844, 10.671875, 0, 0, 0}, {"Police", 2060, 1369.6892089844, 10.671875, 2020, 1369.6892089844, 10.671875, 0, 0, 0} } counter = 0 addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), function() for k, v in ipairs(TeamTable) do counter = counter + 1 TableTeamName = TeamTable[k][1] theGate = createObject(988, TeamTable[k][2], TeamTable[k][3], TeamTable[k][4], TeamTable[k][8], TeamTable[k][9], TeamTable[k][10]) teamMarker = createMarker(TeamTable[k][2], TeamTable[k][3], TeamTable[k][4] - 2, "cylinder", 5, 255, 0, 0, 150) end outputChatBox("found "..counter.." gate", root) end) addEventHandler("onMarkerHit", root, function(player) if (source == teamMarker and getElementType(player) == "player") then outputChatBox("hit") local playerTeam = getPlayerTeam(player) if playerTeam then outputChatBox("team") local teamName = getTeamName(playerTeam) if teamName == TableTeamName then outputChatBox("yee") else outputChatBox("You are not in this team") end else outputChatBox("not in team") end end end) Edited December 4, 2017 by TheMOG Link to comment
Moderators IIYAMA Posted December 5, 2017 Moderators Share Posted December 5, 2017 function getTeamDataFromName (name) for i=1, #TeamTable do if TeamTable[i][1] == name then return TeamTable[i] end end return false end Usage: local teamName = getTeamName(playerTeam) if teamName then local teamData = getTeamDataFromName (teamName) -- returns the sub-table if successfull, else false. if teamData then -- do your thing end end There are more ways to do this. 1 Link to comment
pa3ck Posted December 5, 2017 Share Posted December 5, 2017 Because you keep overwriting the variables, if you have 5 gates, which gate do you think will open? Always the last one, since every time you go into the loop, a new value is assigned. for k, v in ipairs(TeamTable) do counter = counter + 1 TableTeamName = TeamTable[k][1] -- theGate and teamMarker are the variables being overwritten theGate = createObject(988, TeamTable[k][2], TeamTable[k][3], TeamTable[k][4], TeamTable[k][8], TeamTable[k][9], TeamTable[k][10]) teamMarker = createMarker(TeamTable[k][2], TeamTable[k][3], TeamTable[k][4] - 2, "cylinder", 5, 255, 0, 0, 150) end Since you have multiple markers and gates, you should store them in a table. local availableGates = {} for k, v in ipairs(TeamTable) do counter = counter + 1 local team = TeamTable[k][1] local theGate = createObject(988, TeamTable[k][2], TeamTable[k][3], TeamTable[k][4], TeamTable[k][8], TeamTable[k][9], TeamTable[k][10]) local teamMarker = createMarker(TeamTable[k][2], TeamTable[k][3], TeamTable[k][4] - 2, "cylinder", 5, 255, 0, 0, 150) table.insert(availableGates, {theGate, teamMarker, team}) end function getTeamGateMarker( markerElement ) for k, v in ipairs(availableGates) do if(v[2] == markerElement) then return v end end return false end addEventHandler("onMarkerHit", resourceRoot, function(player) local gate = getTeamGateMarker(source) if (gate and getElementType(player) == "player") then local playerTeam = getPlayerTeam(player) if playerTeam then outputChatBox("team") local teamName = getTeamName(playerTeam) if teamName == gate[3] then outputChatBox("yee") else outputChatBox("You are not in this team") end else outputChatBox("not in team") end end end) Try something like that, keep in mind I haven't touched MTA or LUA in ages.. 1 Link to comment
itHyperoX Posted December 5, 2017 Author Share Posted December 5, 2017 Thank you, one more question. How can i move the gate ? addEventHandler("onMarkerHit", resourceRoot, function(player) local gate = getTeamGateMarker(source) if (gate and getElementType(player) == "player") then local playerTeam = getPlayerTeam(player) if playerTeam then outputChatBox("team") local teamName = getTeamName(playerTeam) if teamName == gate[3] then moveObject ( theGate, 10000, TeamTable[k][4], TeamTable[k][5], TeamTable[k][6] ) outputChatBox("yee") else outputChatBox("You are not in this team") end else outputChatBox("not in team") end end end) Link to comment
Storm-Hanma Posted December 5, 2017 Share Posted December 5, 2017 8 minutes ago, TheMOG said: Thank you, one more question. How can i move the gate ? addEventHandler("onMarkerHit", resourceRoot, function(player) local gate = getTeamGateMarker(source) if (gate and getElementType(player) == "player") then local playerTeam = getPlayerTeam(player) if playerTeam then outputChatBox("team") local teamName = getTeamName(playerTeam) if teamName == gate[3] then moveObject ( theGate, 10000, TeamTable[k][4], TeamTable[k][5], TeamTable[k][6] ) outputChatBox("yee") else outputChatBox("You are not in this team") end else outputChatBox("not in team") end end end) I suggest u to use dutuchman moving gate resource resource which is easy to use Link to comment
pa3ck Posted December 5, 2017 Share Posted December 5, 2017 Well, it depends how you want it to work. If you only want it to open then close after X amount of time, throw in two moveObjects, but you also have to think about the marker, what should happen if the gate is moving and somebody else (or the same guy) enters the marker again. You can put a "state" variable as the 4th argument of the table and when it opens/closes update it's state. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now