Right now it seems to have an issue where you have to restart the resrouce everytime someone joins. If anyone knows how to have the loop reset when a player connects let me know! Is there a way in a server.lua to restart a loop in a client lua? or even in the client lua itself? Via a playerconnect event?
client scripts run on every client independently of any other clients scripts. It’s possible that clients could have an issue because you have started operations in the looping thread prior to the session becoming fully active?
Try to insert this as the first line above your while true do loop. I usually always include these on any threads in a client script to make sure the player is active in session first before executing code.
Citizen.CreateThread(function()
while not NetworkIsPlayerActive(PlayerId()) do
Citizen.Wait(0)
end
while true do
...
Can you try to change the script to this @davewazere and see if it works? Not sure really why the script would not run on new clients joining but what I did below was wrote in what I suggested and also changed your player coords to x,y,z and used these in the GetDistanceBetweenCoords native just in case. Your next step should be to show your citizen.log file if you can reproduce the issue on your own game, otherwise ask someone else that joins that has the issue to send you theirs. Check it for errors.
Citizen.CreateThread(function()
while not NetworkIsPlayerActive(PlayerId()) do
Citizen.Wait(0)
end
while true do
Citizen.Wait(0)
local player = GetPlayerPed( -1 )
local x,y,z = table.unpack(GetEntityCoords(player, true))
local inzone = 0
local dist = GetDistanceBetweenCoords(x,y,z,1847.916015625,3675.8190917968,33.767009735108,true)
if dist <= 50.0 then
inzone = 1
if not notifIn then
TriggerEvent("pNotify:SendNotification",{
text = "<b style='color:#1E90FF'>You are in a SafeZone</b>",
type = "success",
timeout = (3000),
layout = "bottomcenter",
queue = "global"
})
notifIn = true
notifOut = false
end
elseif dist >= 50.1 then
inzone = 0
if not notifOut then
TriggerEvent("pNotify:SendNotification",{
text = "<b style='color:#1E90FF'>You are in NO LONGER a SafeZone</b>",
type = "error",
timeout = (3000),
layout = "bottomcenter",
queue = "global"
})
notifOut = true
notifIn = false
end
end
if inzone == 1 then
SetEntityInvincible(player,true)
elseif inzone == 0 then
SetEntityInvincible(player,false)
end
end
end)
Right now it seems to have an issue where you have to restart the resrouce everytime someone joins. If anyone knows how to have the loop reset when a player connects let me know! Is there a way in a server.lua to restart a loop in a client lua? or even in the client lua itself? Via a playerconnect event?
I’d suggest finding why you need to do that because that’s weird. I am obligated to tell you that this is very bad practice. But for now you can hard-code it to do so, yes:
Client.lua:
local firstSpawn = true
AddEventHandler("playerSpawned", function(spawn)
if firstSpawn then
TriggerServerEvent('restartZones')
firstSpawn = false
end
end)
What do you mean a field? Like a marker?(hologram circle thing?) If you use vRP you can just add your own marker in the blipsandmarkers lua. From in there you can make the x and y radius. Color, height, alpha, all that good stuff.
My bad, the line should look like this, edited my post above to reflect.
local x,y,z = table.unpack(GetEntityCoords(player, true))
Do you have the same issue if you use the version without pNotify, like if you just send messages to chat instead? Wondering if this resource should have
dependency 'pNotify' in the __resource.lua file or if you have this resource starting in server.cfg before pNotify…
What do you mean it duplicates it? What?
Does it just show it again when someone spawns? That’s going to happen because the script is restarted, thus resetting the variables.
No no my friend. Instead of it just sending one Noty, it sends 2 when you leave or enter the zone now, simultaneously UNLESS you manual restart. It really doesnt like us. If you need I can go record. Ive tried to move the handler above, below, inside of the function. Changed it from true/false to integers to see if that would help. Nothing.
Alright so Ive tried yours as well again and still to no avail. Ive also tried to add in NetworkSetFriendlyFireOption(true/false) along with and replacing the GodMode native. Still nothing other than the notifications. Only when you restart the resource from console does the FF and god mode take effect. I dont know how much more i can scratch my head before i go bald! Ive tried to create a seperate Citizen.CreateThread as well, attempting separate the calling of your location and the actually setting of godmode/ff
Heres what we got
-- I am assuming the distance is in meters? No idea honestly. All you have to do is change the coords and the
-- distance which you want to be covered. The distance is the RADIUS of a circle. So if you do 50, it will be a 100(unit) diameter.
local inzone = 0
local player = GetPlayerPed( -1 )
Citizen.CreateThread(function()
while not NetworkIsPlayerActive(PlayerId()) do
Citizen.Wait(0)
end
while true do
Citizen.Wait(0)
local x,y,z = table.unpack(GetEntityCoords(player, true))
local dist = GetDistanceBetweenCoords(x,y,z,1847.916015625,3675.8190917968,33.767009735108,true)
if dist <= 50.0 then
inzone = 1
if not notifIn then
TriggerEvent("pNotify:SendNotification",{
text = "<b style='color:#1E90FF'>You are in a SafeZone</b>",
type = "success",
timeout = (3000),
layout = "bottomcenter",
queue = "global"
})
notifIn = true
notifOut = false
end
elseif dist >= 50.1 then
inzone = 0
if not notifOut then
TriggerEvent("pNotify:SendNotification",{
text = "<b style='color:#1E90FF'>You are in NO LONGER a SafeZone</b>",
type = "error",
timeout = (3000),
layout = "bottomcenter",
queue = "global"
})
notifOut = true
notifIn = false
end
end
if inzone == 1 then
SetEntityInvincible(player,true)
NetworkSetFriendlyFireOption(false)
elseif inzone == 0 then
SetEntityInvincible(player( -1 ),false)
NetworkSetFriendlyFireOption(true)
end
end
end)