Jump to content

bindKey on server side


Hiding

Recommended Posts

Hello guys, I was wondering how can I approach this: so if a player presses the f button, and if it was the source, who pressed the button then it should say "you pressed the button" otherwise "playername pressed the f button". My question is how can i get the owner of the presser? So if I press the F button then I should know that it was by me, but for others they should know that it was also me. I hope it's clear :D I need the the source's localPlayer on server side. (I guess)

-- client side
bindKey("f", "down", function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	
    if vehicle then	
        triggerServerEvent("onPlayerToggledInvisibility", localPlayer, vehicle, localPlayer)
    end
end)

-- server side
function onPlayerToggledInvisibility(vehicle, localPlayer)

    if vehicle then   
		if source == localPlayer then
			outputChatBox("you pressed the f button")
		else
			outputChatBox(getPlayerName(source) .. "pressed the f button")
		end
	end
end
addEvent("onPlayerToggledInvisibility", true)
addEventHandler("onPlayerToggledInvisibility", root, onPlayerToggledInvisibility)

 

Link to comment
-- client side
bindKey("f", "down", function()
	local vehicle = getPedOccupiedVehicle(localPlayer)
	
	if vehicle then
		triggerServerEvent("onPlayerToggledInvisibility", localPlayer, vehicle)
	end
end)

-- server side
function onPlayerToggledInvisibility(vehicle)
    if vehicle then
		local list = getElementsByType('player') -- we need to get every player so when can give a different message
		for i, player in pairs(list) do
			if player ~= client then -- if player we get is different from the one that trigger the server event
				outputChatBox(getPlayerName(client) .. "pressed the f button", player) --to other player will show in chat that someplayer press F button
			else
				outputChatBox("you pressed the f button", client) -- to the player who pressed will show you pressed
			end
		end
	end
end

its not good send localPlayer as argument to triggerServerEvent. If you want to use who trigger's it, on server side you can use client variable.

Edited by Rougue90
  • Like 1
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...