Jump to content

setElementVisible error


Hero192

Recommended Posts

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 by Guest
Link to comment
   
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

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

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
  • Moderators

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...