[SOLVED] Difference between client and server scripts (setting player hp as admin)

Looked at many scripts and can’t seem to figure out when to use a server script vs a client script. Currently using a server script to load commands for example the command.

function CMD_sethp(source, args, help)
	local arguments = argsToLocals(args)
	if parseInput("si",arguments) then
		local zplayer, zhealth = table.unpack(arguments)
		TriggerClientEvent('chatMessage', source, '[ADMIN]', { 0, 255, 0 }, 'You have set ' .. zplayer .. ' health to ' .. zhealth)
		SetEntityHealth(zplayer,zhealth)
	else
		TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, '/sethp [playername] [health]')
	end
end

why doesnt this work?
it shows the chat message but never sets the health

I’m probably not going to explain this very well, but I will try my best!

The client and server as two completely separate things. The server code is well… the server. The client code is the code that is running on each and every client. The server has no idea about the game. The server only relays internet traffic between the clients. The server can tell clients what to do by triggering events on the client(s).

You can only use native GTA functions on the client side (because the server doesn’t know anything about the game). What you would need to do is create an event handler in the client code for setting the health. Then, you will need to trigger it from the server on what client you want it to trigger on. You are already triggering the event ‘chatMessage’ on the specific client. You just need another event for setting health and put that native (SetEntityHealth) there.

If that doesn’t make any sense, think of the server as a traffic light and think of the clients as all of the cars on the road. The cars can do different things like accelerate, brake, turn, etc. The traffic light can send a message to the cars for them to do these things.

If I can explain this a little better, let me know.

4 Likes

Thanks a lot for that, I just today started to grasp the concept of server to client communication.

What you would need to do is create an event handler in the client code for setting the health. Then, you will need to trigger it from the server on what client you want it to trigger on.

This is exactly what I would like to know how to do. How would I go about adding EventHandler to my server script then triggering it on my client script ONLY when the command is typed. Thanks a lot. Feel free to use another example besides the whole /sethp if you need to.

client code:RegisterNetEvent("setThisGuysHP") AddEventHandler("setThisGuysHP", function(hp) SetEntityHealth(GetPlayerPed(-1), hp) end)

server code:function CMD_sethp(source, args, help) local arguments = argsToLocals(args) if parseInput("si",arguments) then local zplayer, zhealth = table.unpack(arguments) TriggerClientEvent('chatMessage', source, '[ADMIN]', { 0, 255, 0 }, 'You have set ' .. zplayer .. ' health to ' .. zhealth) TriggerClientEvent('setThisGuysHP', source, zhealth) else TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, '/sethp [playername] [health]') end end

Assuming the rest of your code is functional, that should trigger the event to set the client’s HP.

But yeah… I started coding in PHP so it was just server, not this client/server stuff. The client/server relationship completely fucked my brain when I started learning it.

2 Likes

Hmm, something strange is happening.

The command /sethp isn’t working.

Server Side

function CMD_sethp(source, args, help)
	local arguments = argsToLocals(args)
	if parseInput("si",arguments) then
		local zplayer, zhealth = table.unpack(arguments)
		TriggerClientEvent('chatMessage', source, '[ADMIN]', { 0, 255, 0 }, 'You have set ' .. zplayer .. ' health to ' .. zhealth)
		TriggerClientEvent('setThisGuysHP', source, zhealth)
	else
		TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, '/sethp [playername] [health]')
	end
end

Client Side

RegisterNetEvent("setThisGuysHP")
AddEventHandler("setThisGuysHP", function(hp)
    SetEntityHealth(GetPlayerPed(-1), hp)
end)

However this quick /kill command I made is working.

Server Side

function CMD_kill(source, args, help)
	TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, 'kys')
	TriggerClientEvent('killSelf', source)
end

Client Side

RegisterNetEvent("killSelf")
AddEventHandler("killSelf",function()
	SetEntityHealth(GetPlayerPed(-1), 0)
end)

I would think that zhealth isn’t being passed correctly but it is because it lists the number correctly in the chatMessage.

If I understand correctly, zhealth is being set by the text the player enters, right? If so, do zhealth = tonumber(zhealth) after local zplayer, zhealth = table.unpack(arguments)
The code is trying to read the health as a string, but that won’t work, because it needs to be an integer. At least I think that is the issue.

1 Like

Also change this to TriggerClientEvent('setThisGuysHP', zplayer, zhealth)
The code I gave you will trigger that on the person who typed the command in. I assume you want to specify a player.

1 Like

Yeah the command would basically be “/sethp playername hpToSetTo”

function CMD_sethp(source, args, help)
	local arguments = argsToLocals(args)
	if parseInput("si",arguments) then
		local zplayer, zhealth = table.unpack(arguments)
		zhealth = tonumber(zhealth)
		TriggerClientEvent('chatMessage', source, '[ADMIN]', { 0, 255, 0 }, 'You have set ' .. zplayer .. ' health to ' .. zhealth)
		TriggerClientEvent('setThisGuysHP', zplayer, zhealth)
	else
		TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, '/sethp [playername] [health]')
	end
end
RegisterNetEvent("setThisGuysHP")
AddEventHandler("setThisGuysHP", function(zplayer,zhealth)
    SetEntityHealth(zplayer, zhealth)
end)

Still not working… truly stumped.

Edit:

Error Code -


“against_all_odds” being my steam name

That error (cannot format to dec, or whatever it says) basically means it can’t format the information it is given into a number value. Try using a player ID instead of the name. By default a basic scoreboard resource should be activated on the server. I think the default key to bring it up is up arrow. You can see IDs on there.

1 Like

Alright after some messing with it and general debugging I got half of it working…

Server Script

function CMD_sethp(source, args, help)
	local arguments = argsToLocals(args)
	if parseInput("ii",arguments) then
		local zplayerID, zhealth = table.unpack(arguments)
		zplayerID = tonumber(zplayerID)
		zhealth = tonumber(zhealth)
		TriggerClientEvent('chatMessage', source, '[ADMIN]', { 0, 255, 0 }, 'You have set ' .. zplayerID .. ' health to ' .. zhealth)
		TriggerClientEvent('chatMessage', zplayerID, '[ALERT]', { 0, 255, 0 }, 'Your health has been set my an admin')
		TriggerClientEvent('setThisGuysHP', zplayerID)
	else
		TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, '/sethp [playername] [health]')
	end
end

“Your health has been set my an admin”

Works completely fine and is only sent to the ID selected, the listing of the health amount in chatMessage is also listed fine, however I think the problem lies somewhere in the client script or the communication from server → client.

Client Script

RegisterNetEvent("setThisGuysHP")
AddEventHandler("setThisGuysHP", function(zplayerID,zhealth)

	local player = GetPlayerPed(GetPlayerFromServerId(zplayerID))

	TriggerClientEvent('chatMessage', player, '[ADMIN]', { 0, 255, 0 }, 'msg from client file')
    SetEntityHealth(player, zhealth)

end)

msg from client file

The client script chatMessage isn’t working, nor is the actual SetEntityHealth. Tried several different variations of the client script and nothing has worked so far. Thanks!

When you trigger a client event from the server, it should be like this (‘eventname’, who, arg1, arg2, etc)

The ‘who’ is the player’s server id. That does not carry over to the client side function. It simply tells the server who to trigger it on. If you want it to trigger on everybody, use -1 (not in this case, but for future reference).

function CMD_sethp(source, args, help) local arguments = argsToLocals(args) if parseInput("ii",arguments) then local zplayerID, zhealth = table.unpack(arguments) zplayerID = tonumber(zplayerID) zhealth = tonumber(zhealth) TriggerClientEvent('chatMessage', source, '[ADMIN]', { 0, 255, 0 }, 'You have set ' .. zplayerID .. ' health to ' .. zhealth) TriggerClientEvent('chatMessage', zplayerID, '[ALERT]', { 0, 255, 0 }, 'Your health has been set my an admin') TriggerClientEvent('setThisGuysHP', zplayerID, zhealth) else TriggerClientEvent('chatMessage', source, '[GENERAL]', { 0, 255, 0 }, '/sethp [playername] [health]') end end

[code]RegisterNetEvent(“setThisGuysHP”)
AddEventHandler(“setThisGuysHP”, function(zhealth)

TriggerEvent('chatMessage', '[ADMIN]', { 0, 255, 0 }, 'msg from client file')
    SetEntityHealth(GetPlayerPed(-1), zhealth)

end)[/code]

Have you tried anything like the above? Also, when you call a client event on the client, just use TriggerEvent. Don’t call TriggerClientEvent. That is for calling a client event from the server.

Edit: I’m testing this myself. I’ll edit/reply when/if I get it working.

1 Like

I got it working using the above example, except modified so it works with my server commands. One thing I discovered is that if you set the health under 100, it will auto-kill. Oddly enough, 101 hp is near death. Let me know if you get it working.

1 Like

Holy shit it worked lol, thanks a lot. However I have a few questions about why exactly it does work.

Client Side Script

RegisterNetEvent("setThisGuysHP")
1 AddEventHandler("setThisGuysHP", function(zhealth) 
   2 TriggerEvent('chatMessage', '[ADMIN]', { 0, 255, 0 }, 'msg from client file') - why do we also not include zplayerID here to direction the msg toward them?
  3  SetEntityHealth(GetPlayerPed(-1), zhealth)  - and what does GetPlayerPed(-1) mean exactly the player that is having the event triggered on them? in this case zplayerID
end)

1 AddEventHandler(“setThisGuysHP”, function(zhealth)

  • why do we not include zplayerID here as the first parameter?

2 TriggerEvent(‘chatMessage’, ‘[ADMIN]’, { 0, 255, 0 }, ‘msg from client file’)

  • why do we also not include zplayerID here to direct the msg toward them?

3 SetEntityHealth(GetPlayerPed(-1), zhealth)

what does GetPlayerPed(-1) mean exactly (because I see it used everywhere), the player that is having the event triggered on them? in this case zplayerID?

So, when you are writing this code, the client and server files are only in one file. There is only 1 server, and that links all of the clients together. However, there are multiple clients, and each and every single one of those clients is running its own version of that client code. Every client has its own client.lua running in their game. It is just copying what you wrote and sending it to every client when they join.

  1. Because you already told which client to run the code on from the server with the TriggerClientEvent. That zplayerID says “Hey, Eddie, set your health to what I tell you.” That’s done by the server. Now, it wouldn’t make sense for you to tell yourself “Eddie, set your health”, right? You already received the message to set yourself, why do you need to specify who needs to do it again?

  2. Same as 1

  3. That just gets that client’s player ped.

I think the way you are understanding it is that the client code is handling every single client from one instance, but that’s not the case. The client code is running on every single client in its own instance. I’m not sure how to better explain it. Let me know if you still can’t understand and I will try :slightly_smiling_face:

2 Likes

1 AddEventHandler(“setThisGuysHP”, function(zhealth)

So the arguments would go into the (), which is why zplayerID isn’t in there but zhealth is, and if we had more arguments for our command then they would follow zhealth?

Correct ------------

1 Like

Thanks for the help, solved.

1 Like

No problem! --------

Maybe a dumb question then since i found this topic by the good search option :yum:
What about the area code / map.

If you have it client side, am i the only one seeing it ?
I’m so confused LOL

@Dominic1337 could you maybe post the resource you’re talking about in this forum on here or release it in a different forum as a release? Because that script would be very useful to me and I tried copy pasting the parts you posted in different messages but I can’t get it to work