[HELP] What is wrong with my code?

This is my client

function JailPlayer()
	t, distance = GetClosestPlayer()
	local jailed = freezeEntityPosition(t, false)
	if(distance ~= -1 and distance < 3) then
		jailed = true --Player Er jailet
	elseif (jailed == true)
	jailed = false -- Player er allerede jailet, unjail ham...
 end
end
function Jail()
	t, distance = GetClosestPlayer()
	if(distance ~= -1 and distance < 3) then
		TriggerServerEvent("police:Jail", GetPlayerServerId(t))
		TriggerEvent('chatMessage', 'GOVERNMENT', {255, 0, 0}, "Ingen Spiller i nĂŚrheden !")
	else
		TriggerEvent('chatMessage', 'GOVERNMENT', {255, 0, 0}, GetPlayerName(t).. "er blevet Jailet!")
	end
end

This is my Server

RegisterServerEvent('police:Jail')
AddEventHandler('police:Jail', function(t)
	TriggerClientEvent('chatMessage', source, 'GOVERNMENT', {255, 0, 0}, JailPlayer(t))
end)

Take alook at this guys code

May be of help

You cant call a function from serverside thats in clientside without it being inside a registered event handler.

and why are you trying to call a function inside a chat message that requires it to return a string when the function you listed above has no section to return anything

I don’t wont to use another cop script, i wont to make my own work.

Yea I understand

You would have to do something like this

RegisterNetEvent("jail:jailplayer")
AddEventHandler("jail:jailplayer", function( test )
function JailPlayer()
	t, distance = GetClosestPlayer()
	local jailed = freezeEntityPosition(t, false)
	if(distance ~= -1 and distance < 3) then
		jailed = true --Player Er jailet
	elseif (jailed == true)
	jailed = false -- Player er allerede jailet, unjail ham...
 end
end
end)

and serverside you would have to do this

RegisterServerEvent('police:Jail')
AddEventHandler('police:Jail', function(t)
	TriggerClientEvent('jail:jailplayer', source,  t)
end)

I dont know if your code will work or not, but that is the correct way to do things, and if you need to pass variables to other players you would create a separate lua file with them in it then do this

RegisterNetEvent("jail:varsync")
AddEventHandler("jail:varsync", function(thisvariable, var2, var3)
     jail.thisvariable = thisvariable
     jail.var2 = var2
     jail.var3 = var3
end)

and serverside you would do this

RegisterServerEvent('jail:sync')
AddEventHandler('jail:sync', function(thisvariable, var2, var3)

TriggerClientEvent("jail:varsync", -1, thisvariable, var2, var3)



the external file would look like this


jail = {

thisvariable = true, 
var2 = something, 
var3 = something,

}



1 Like

You confused me now :smiley: i understood everything, untill the variables things.

But it works ingame now, but the player doesnt gets frozen.

Is this worng?

t, distance = GetClosestPlayer()
local jailed = freezeEntityPosition(t, false)
	if(distance ~= -1 and distance < 3) then
		jailed = true --Player Er jailet
	elseif (jailed == true)
	jailed = false

Do i taget the player the right way?

t, distance = GetClosestPlayer()

BTW THANKS SO MUCH FOR ALL THIS :smiley:

GetClosestPlayer() is not a native function in fivem you can do this though

local x, y, z = GetEntityCoords(GetPlayerPed(-1), true)
local radius = how far you want this to be
local t = GetClosestPed(x, y, z, radius, true, false, false, false, 1)

the only problem is that this only works if the ped is walking near you for some reason… ill try to find a better solution.

ahhh i found out why you was using that. you took it out of the cop script anyway you gotta take this with it, put this in your client file

function GetClosestPlayer()
	local players = GetPlayers()
	local closestDistance = -1
	local closestPlayer = -1
	local ply = GetPlayerPed(-1)
	local plyCoords = GetEntityCoords(ply, 0)
	
	for index,value in ipairs(players) do
		local target = GetPlayerPed(value)
		if(target ~= ply) then
			local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
			local distance = GetDistanceBetweenCoords(targetCoords["x"], targetCoords["y"], targetCoords["z"], plyCoords["x"], plyCoords["y"], plyCoords["z"], true)
			if(closestDistance == -1 or closestDistance > distance) then
				closestPlayer = value
				closestDistance = distance
			end
		end
	end
	
	return closestPlayer, closestDistance
end

Thanks dude, but the players still doesnt frezze ?

and why do you pass in test here?

AddEventHandler("jail:jailplayer", function( test )

shuld it not be (t) ?

Why are you doing local jailed = FreezeEntityPosition. Then you’re doing jailed = true. I know Lua doesn’t have types like other languages but, this is just taking the mick :stuck_out_tongue:

Anyways, the native you’re trying to use doesn’t have a return type so, you don’t need to do jailed = freezeEntityPosition. See here.

As other have said, you should not be trying to call the client function from the server code unless it’s through an event. So, let’s see if we can make this a bit cleaner. Let’s assume you’re using a command to jail someone, you would need to first get the closest player to jail from the cop. So, you could do something like this on the server (when the command is executed).

Note: this is pseudocode and won’t work out-of-the-box.

On command do {
    TriggerClientEvent("jail:jailClosestPlayer", source)
}

Then, on the client you would have to register and handle the event. In the handler you could either write the logic inside it or, you could call your jail function.

RegisterNetEvent("jail:jailClosestPlayer")
AddEventHandler("jail:jailClosestPlayer", function()
    JailClosest()
end)

-- or 
RegisterNetEvent("jail:jailClosestPlayer")
AddEventHandler("jail:jailClosestPlayer", function()
        t, distance = GetClosestPlayer()

	if(distance ~= -1 and distance < 3) then
		TriggerServerEvent("jail:jailPlayer", GetPlayerServerId(t)) -- send server Id to freeze
	end
end) 

Then you would have to handle that event on the server, probably triggering yet another client event (i don’t know if its possible but, you might be able to do TriggerClientEvent("event", GetPlayerServerId(t)) but, I’m not too sure). Looking something like

On event "jail:jailPlayer" (int target) {
    TriggerClientEvent("jail:jailPlayerClient", target)
}

The your final event would handle freezing the player and doing other stuff. E.g.

local isInJail = false; -- is player in jail?
On event "jail:jailPlayerClient"{
   isInJail = not isInJail
    FreezeEntityPosition(GetPlayerPed(-1), isInJail)
}

Phew. That was a lot of text :smile:. I’m on phone so, code won’t be good and there will almost certainly be typos.

Wow heavy comment right there, thanks dude…

Still im very new, and doesn’t understand how to make theese “On Event” or understrand terms like (int target).

Why you pass source here?

TriggerClientEvent("jail:jailClosestPlayer", source)

ok, before going deep into coding by making a jail script simply make a script that on a command does something like gets the players coords and says them in chat to only him.

The “on event” stuff was just short hand for

RegisterServerEvent("event name") 
-- or RegisterNetEvent("event name")
AddEventHandler("event name", function()

end)

Or, in the case of the event with (int target) it would be

RegisterSeverEvent("jail:jailPlayer")
AddEventHandler("jail:jailPlayer", function(target --[[ should be an int]])
 
end)

I assumed that you were triggering the whole thing from a server event (e.g. chatMessage) so, you would have to pass source to get the closest player to the guy who triggered the event. E.g. if you triggered the chatMessage event, it would get the closest player to you.

Any tutorials on how to use Client and Server, whit different event handlers? :slight_smile:)

Not that I’m aware of however, the wiki does have some examples :

https://wiki.fivem.net/wiki/Main_Page

Edit: I’d take the advise of @rjross2013 and start with something basic to get the hand of stuff before trying to create a semi-complicated script such as a jail script. :slight_smile: