[Release] Safe Zone(s) Updated 9/10/18

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?

@anders said

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
...

But this didnt seem to have any effect on mine :confused:

perfect i will use it, thx for sharing dude!

Maybe you can include a field around, config show yes / no?

Nice worked- i testing on my server too! )
Very Thx!

1 Like

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)

Ill give that a go when i get home from work tonight. Thanks again for your help!

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)

Server.lua:

RegisterServerEvent('restartZones')
AddEventHandler('restartZones')
	ExecuteCommand("restart resourceName")
end)

Lemme know if it doesn’t work. Once again, haven’t tested.

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. :slight_smile:

So the one with the table did NOT work. :confused:

The event handler did not work either, I still had to manually restart the resource via the console.

This is quite the conundrum

Anyone manage to make multiple zones?
Had some issues doing this

Im more so focused on making it so you dont have to manual restart the damn thing. Once I get this tackled Ill be glad to help with multiple zones

Ah yeah that’s an issue

Coule be awesome if there was more than one place for safezone

1 Like

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…

I want to not show the weapon instead of killing.

1 Like

Woops. I forgot to register the server event. Try this as the server.lua (the earlier client.lua i posted should work fine):

RegisterServerEvent('restartZones')
AddEventHandler('restartZones')
    print("Restarting resource")
	ExecuteCommand("restart resourceName")
end)

And obviously replace “resourceName” wiith the name of the resource.

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.

1 Like

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.

Ok yeah that makes no sense. I don’t think it’s my code causing this.

Regardless, it TECHNICALLY works. Just adds an extra Noty. So Ill just have to focus on this duplication thing. Thanks for all your help!

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)