Hero192 Posted August 21, 2015 Share Posted August 21, 2015 (edited) Hey guys, i tried this way to make the blip visible only for criminals it works fine ,but it gives WARNINGs, please anyone can help? i want to remove this warning from the code WARNING:Bad argument @ 'setElementVisibleTo' [Expected element at argument 1, got nil] local Vehicle = createVehicle(522,1825.94, -872.12, 63,0,0,0) local criminalBlip = createBlipAttachedTo ( Vehicle, 51 ) setTimer(function () setElementVisibleTo(criminalBlip, root, false) for k, player in ipairs (getElementsByType("player")) do if ( getTeamName ( getPlayerTeam (player) ) == "Criminal" ) then setElementVisibleTo(criminalBlip, player, true) end end end, 1000, 0) setTimer(function () for k, player in ipairs (getElementsByType("player")) do if ( getTeamName ( getPlayerTeam (player) ) ~= "Criminal" ) then setElementVisibleTo(criminalBlip, player, false) end end end, 1000, 0) Edited August 21, 2015 by Guest Link to comment
Walid Posted August 21, 2015 Share Posted August 21, 2015 post full code here , as i can see criminalBlip it's not defined in your code. Link to comment
Hero192 Posted August 21, 2015 Author Share Posted August 21, 2015 local Vehicle = createVehicle(522,1825.94, -872.12, 63,0,0,0) local criminalBlip = createBlipAttachedTo ( Vehicle, 51 ) setTimer(function () setElementVisibleTo(criminalBlip, root, false) for k, player in ipairs (getElementsByType("player")) do if ( getTeamName ( getPlayerTeam (player) ) == "Criminal" ) then setElementVisibleTo(criminalBlip, player, true) end end end, 1000, 0) setTimer(function () for k, player in ipairs (getElementsByType("player")) do if ( getTeamName ( getPlayerTeam (player) ) ~= "Criminal" ) then setElementVisibleTo(criminalBlip, player, false) end end end, 1000, 0) Link to comment
JR10 Posted August 21, 2015 Share Posted August 21, 2015 Whenever the blip is destroyed (can be the vehicle) the criminalBlip will refer to nil. You use isElement to make sure that the criminalBlip is still there. Using two infinite timers is very bad. Just use events. Link to comment
Hero192 Posted August 21, 2015 Author Share Posted August 21, 2015 So, please tell me which events should i use and replace them with the infinity timer? Link to comment
JR10 Posted August 21, 2015 Share Posted August 21, 2015 Well, there's no event for when a player changes teams, but you can trigger one whenever your script uses setPlayerTeam. Link to comment
Hero192 Posted August 21, 2015 Author Share Posted August 21, 2015 Something like that can be usefull ? addEventHandler ( "onPlayerTeamChange", root, function ( oldTeam,newTeam ) local team = getTeamFromName ( "Criminal" ) if oldTeam == team then setElementVisibleTo( criminalBlip, source, nil ) elseif newTeam == team then setElementVisibleTo( criminalBlip, source, true ) end end) local lastPlayerTeam = {} setTimer(function () local players = getElementsByType("player") for i=1,#players do local player = players[i] local playerTeam = getPlayerTeam(player) or false -- make sure it always returns false, if it returns nil. if playerTeam then local lastTeam = lastPlayerTeam[player] or false -- make sure it always returns false, if it returns nil. if playerTeam ~= lastTeam then triggerEvent("onPlayerTeamChange",player,isElement(lastTeam) and lastTeam or false,playerTeam) lastPlayerTeam[player] = playerTeam end else local lastTeam = lastPlayerTeam[player] if lastTeam then triggerEvent("onPlayerTeamChange",player,isElement(lastTeam) and lastTeam or false,nil) lastPlayerTeam[player] = nil end end end end,1000,0) addEventHandler("onPlayerQuit",root, function () if lastPlayerTeam[source] then lastPlayerTeam[source] = nil end end) Link to comment
Hero192 Posted August 21, 2015 Author Share Posted August 21, 2015 I tried that but it doesn't working at all, ERROR: Loading script failed : unexpected symbol near ' --Line 3 addEventHandler ( "onPlayerTeamChange", root, function ( oldTeam,newTeam ) local team = getTeamFromName ( "Criminal" ) if oldTeam == team then setElementVisibleTo( criminalBlip, source, nil ) elseif newTeam == team then setElementVisibleTo( criminalBlip, source, true ) end end) local lastPlayerTeam = {} setTimer(function () local players = getElementsByType("player") for i=1,#players do local player = players[i] local playerTeam = getPlayerTeam(player) or false -- make sure it always returns false, if it returns nil. if playerTeam then local lastTeam = lastPlayerTeam[player] or false -- make sure it always returns false, if it returns nil. if playerTeam ~= lastTeam then triggerEvent("onPlayerTeamChange",player,isElement(lastTeam) and lastTeam or false,playerTeam) lastPlayerTeam[player] = playerTeam end else local lastTeam = lastPlayerTeam[player] if lastTeam then triggerEvent("onPlayerTeamChange",player,isElement(lastTeam) and lastTeam or false,nil) lastPlayerTeam[player] = nil end end end end end,1000,0) addEventHandler("onPlayerQuit",root, function () if lastPlayerTeam[source] then lastPlayerTeam[source] = nil end end) Link to comment
JR10 Posted August 21, 2015 Share Posted August 21, 2015 Is this the full code? I can't spot the syntax error. Try looking at line 3 for any extra symbols. Link to comment
Hero192 Posted August 21, 2015 Author Share Posted August 21, 2015 Yes it's the full code,i don't know how to handle this synax error thats why im asking Link to comment
JR10 Posted August 21, 2015 Share Posted August 21, 2015 I ran the code myself and there doesn't seem to be an error anywhere. Link to comment
Hero192 Posted August 21, 2015 Author Share Posted August 21, 2015 I'll send you the full code via pm Link to comment
xeon17 Posted August 21, 2015 Share Posted August 21, 2015 Yes it's the full code I'll send you the full code via pm Link to comment
Moderators Citizen Posted August 22, 2015 Moderators Share Posted August 22, 2015 PM from the past week: if you give me an example I gave you an example on how to use my script ... Are you really trying to understand ? First get my script that adds the onPlayerTeamChange event and the marker for team visibility system: ------------------------------------- ---- /!\ DO NOT MODIFY BELOW /!\ ---- ------------------------------------- -- This function will get all blips that should be visible for a team and will show it for that entire team and will hide it for all others function updateTeamBlipsVisibility() -- DO NOT MODIFY for k, blip in ipairs(getElementsByType("blip")) do local blipTeam = getElementData(blip, "visibleForTeam") if blipTeam and getElementType(blipTeam) == "team" then clearElementVisibleTo(blip) -- reset the visibility settings for the blip setElementVisibleTo(blip, root, false) -- hide for everyone setBlipVisibleForTeam(blip, blipTeam) -- show for the entire team the blip is associated with end end end -- show a blip for all members of a given team element function setBlipVisibleForTeam(blip, team) -- DO NOT MODIFY for i, player in ipairs( getPlayersInTeam(team) ) do setElementVisibleTo(blip, player, true) end end ------- Logic for the onPlayerChangeTeam event ------- addEvent("onPlayerChangeTeam", true) local _playersTeamCache = {} function onPlayerChangeTeamWatcher() -- DO NOT MODIFY for i, player in ipairs(getElementsByType("player")) do local pTeam = getPlayerTeam(player) local oldTeam = _playersTeamCache[player] or false if pTeam and pTeam ~= oldTeam then -- If he changed team triggerEvent("onPlayerChangeTeam", player, oldTeam, pTeam) _playersTeamCache[player] = pTeam end end end setTimer(onPlayerChangeTeamWatcher, 600, 0) --[[ you can change the "sensibility" of the team change detection: the lower the timer is, the faster the event will be triggered but it can produces lags on a big amount of players and blips ]] addEventHandler("onPlayerQuit", root, function () _playersTeamCache[source] = nil end) ------------------------------------------------------ -- You can modify below Then just do like my example: So to make a blip visible for only one team you need 3 steps: local blip = createBlip(x, y, 0) -- 1: create the blip as usual setElementData(blip, "visibleForTeam", team) -- 2: says for which team this blip will be visible for updateTeamBlipsVisibility() -- 3: call an entire refresh So in your case: local criminalTheftBlip = createBlipAttachedTo(crimainlTheftVehicle, 12) -- 1: create the blip as usual setElementData(criminalTheftBlip, "visibleForTeam", getTeamFromName("Criminal")) -- 2: says for which team this blip will be visible for updateTeamBlipsVisibility() -- 3: call an entire refresh -- 4: do whatever you want (here the message for all Criminals) for i, player in ipairs(getPlayersInTeam(getTeamFromName("Criminal"))) do exports['msg']:sendMessage("Car Jacker: There is a car to steal, go pickup it up.", player,238, 44, 44) end See ? it wasn't that hard to use my code for what you wanted to do. Best regards, Citizen There is no errors and it's working fine on my side. Read the error again carefully and find where is the error located at. By the way I tested it again and just realised I forgot to call the updateTeamBlipsVisibility() function when a player joined another team. So here is the backend code again: And PLEASE ! DO NOT MODIFY THE FOLLOWING CODE (except for the milliseconds of the infinite timer). ------------------------------------- ---- /!\ DO NOT MODIFY BELOW /!\ ---- ------------------------------------- ------- Logic for the onPlayerChangeTeam event ------- addEvent("onPlayerChangeTeam", true) local _playersTeamCache = {} function onPlayerChangeTeamWatcher() -- DO NOT MODIFY for i, player in ipairs(getElementsByType("player")) do local pTeam = getPlayerTeam(player) local oldTeam = _playersTeamCache[player] or false if pTeam and pTeam ~= oldTeam then -- If he changed team triggerEvent("onPlayerChangeTeam", player, oldTeam, pTeam) _playersTeamCache[player] = pTeam end end end setTimer(onPlayerChangeTeamWatcher, 600, 0) --[[ you can change the "sensibility" of the team change detection: the lower the timer is, the faster the event will be triggered but it can produces lags on a big amount of players and blips ]] addEventHandler("onPlayerQuit", root, function () _playersTeamCache[source] = nil end) ------------------------------------------------------ ----------- Logic for the team blips system ---------- -- This function will get all blips that should be visible for a team and will show it for that entire team and will hide it for all others function updateTeamBlipsVisibility() -- DO NOT MODIFY for k, blip in ipairs(getElementsByType("blip")) do local blipTeam = getElementData(blip, "visibleForTeam") if blipTeam and getElementType(blipTeam) == "team" then clearElementVisibleTo(blip) -- reset the visibility settings for the blip setElementVisibleTo(blip, root, false) -- hide for everyone setBlipVisibleForTeam(blip, blipTeam) -- show for the entire team the blip is associated with end end end -- show a blip for an entire team it is associated with function setBlipVisibleForTeam(blip, team) -- DO NOT MODIFY for i, player in ipairs( getPlayersInTeam(team) ) do setElementVisibleTo(blip, player, true) end end addEventHandler("onPlayerChangeTeam", root, function () updateTeamBlipsVisibility() end) ---------------------------------------------------- And here is a test case for you to see it in action: => Test case code: createTeam("Criminals") createTeam("Cops") function addRandomBlip(thePlayer, cmd, teamName) local team = getTeamFromName(teamName) if team then local x, y = math.random(-2000, 2000), math.random(-2000, 2000) local blip = createBlip(x, y, 0) setElementData(blip, "visibleForTeam", team) updateTeamBlipsVisibility() end end addCommandHandler("blip", addRandomBlip, false, false) function changeTeam(thePlayer, cmd, teamName) local team = getTeamFromName(teamName) if team then setPlayerTeam(thePlayer, team) end end addCommandHandler("team", changeTeam, false, false) => Test case commands: When you are ingame us the test case as follow: 1 - /blip Criminals to add a random blip on the map for the Criminals team 2 - /blip Cops to add a random blip on the map for the Cops team 3 - /team Criminals to join the Criminals team (it will show you the blip you created at step 1) 4 - /team Cops to join the Cops team (it will hide the blip you created at step 1 and show you the blip you created at step 2 instead). I have tested it again, so if it's not working for you, then you did something wrong. It should also work with a createBlipAttachedTo, you just have to do the setElementData properly on the created blip, and call a refresh: setElementData(blip, "visibleForTeam", team) updateTeamBlipsVisibility() Regards, Citizen Link to comment
Hero192 Posted August 23, 2015 Author Share Posted August 23, 2015 Hey Citizen, i used your first code i didn't modify it, and i did the setElementData CriminalBlip = createBlipAttachedTo ( Vehicle, 51 ) setElementData(CriminalBlip, "visibleForTeam", Criminal) --Like that but it still showing for No Criminal Team, maybe im wrong, can you correct me please? updateTeamBlipsVisibility() --Your code: ------------------------------------- ---- /!\ DO NOT MODIFY BELOW /!\ ---- ------------------------------------- ------- Logic for the onPlayerChangeTeam event ------- addEvent("onPlayerChangeTeam", true) local _playersTeamCache = {} function onPlayerChangeTeamWatcher() -- DO NOT MODIFY for i, player in ipairs(getElementsByType("player")) do local pTeam = getPlayerTeam(player) local oldTeam = _playersTeamCache[player] or false if pTeam and pTeam ~= oldTeam then -- If he changed team triggerEvent("onPlayerChangeTeam", player, oldTeam, pTeam) _playersTeamCache[player] = pTeam end end end setTimer(onPlayerChangeTeamWatcher, 600, 0) --[[ you can change the "sensibility" of the team change detection: the lower the timer is, the faster the event will be triggered but it can produces lags on a big amount of players and blips ]] addEventHandler("onPlayerQuit", root, function () _playersTeamCache[source] = nil end) ------------------------------------------------------ ----------- Logic for the team blips system ---------- -- This function will get all blips that should be visible for a team and will show it for that entire team and will hide it for all others function updateTeamBlipsVisibility() -- DO NOT MODIFY for k, blip in ipairs(getElementsByType("blip")) do local blipTeam = getElementData(blip, "visibleForTeam") if blipTeam and getElementType(blipTeam) == "team" then clearElementVisibleTo(blip) -- reset the visibility settings for the blip setElementVisibleTo(blip, root, false) -- hide for everyone setBlipVisibleForTeam(blip, blipTeam) -- show for the entire team the blip is associated with end end end -- show a blip for an entire team it is associated with function setBlipVisibleForTeam(blip, team) -- DO NOT MODIFY for i, player in ipairs( getPlayersInTeam(team) ) do setElementVisibleTo(blip, player, true) end end addEventHandler("onPlayerChangeTeam", root, function () updateTeamBlipsVisibility() end) ---------------------------------------------------- Link to comment
Hero192 Posted August 26, 2015 Author Share Posted August 26, 2015 Anyone can help me here please? Link to comment
Hero192 Posted August 27, 2015 Author Share Posted August 27, 2015 Bump (Still not solved) 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