Jump to content

Make a client side function visible to Others


Recommended Posts

What you are requesting is known as synchronization, when a client executes a client-side function and you want to share that state across every other player, you must notify the server of the action so that it can tell all players of the change too.

This can be achieved with events using triggerServerEvent and triggerClientEvent.

For instance, the example below displays how a client executing /trunk will run the function setVehicleDoorState and then share it across to to the server which in turn makes it visible for all other players.

Client

addCommandHandler("trunk", function()
    local theVehicle = getPedOccupiedVehicle(localPlayer)
    if not theVehicle then -- If the player is not in a vehicle.
      outputChatBox("You must be in a vehicle to toggle the trunk!", 255, 0, 0)
      return
    end
    
    local newState = (getVehicleDoorState(theVehicle, 1) == 0) and 1 or 0 -- If the trunk is closed, set it to 1 (open) otherwise it must be open, so close it. (state 1)
    setVehicleDoorState(theVehicle, 1, newState) -- Set the trunk state for this client.
    triggerServerEvent("syncVehicleTrunkChange", theVehicle, newState) -- Notify the server to set the newState for every player.
end)

-- This function is called whenever another player toggles a vehicle trunk, it executes the function on this client to update the trunk state
-- to the same as what the source player set it to.
function updateDoorStateFromOtherPlayer(newState)
	setVehicleDoorState(source, 1, newState)
end
addEvent("updateVehicleDoorState", true)
addEventHandler("updateVehicleDoorState", root, updateDoorStateFromOtherPlayer)

 

Server

function handleVehicleDoorSync(newState)
	for i, thePlayer in ipairs(getElementsByType("player") do -- For every online player.
		triggerClientEvent(thePlayer, "updateVehicleDoorState", thePlayer, source, newState) -- Trigger the event to set the vehicle door state to the newState provided.
	end
end
addEvent("syncVehicleTrunkChange", true)
addEventHandler("syncVehicleTrunkChange", root, handleVehicleDoorSync)

 

Alternatively to save yourself this hassle, if the function is shared as such is the case with setVehicleDoorState, then you can just execute it on the server-side in which case it will automatically be synced to every client.

Edited by Skully
  • Thanks 1
  • Confused 1
Link to comment
On 08/10/2021 at 11:42, Skully said:

What you are requesting is known as synchronization, when a client executes a client-side function and you want to share that state across every other player, you must notify the server of the action so that it can tell all players of the change too.

This can be achieved with events using triggerServerEvent and triggerClientEvent.

For instance, the example below displays how a client executing /trunk will run the function setVehicleDoorState and then share it across to to the server which in turn makes it visible for all other players.

Client

addCommandHandler("trunk", function()
    local theVehicle = getPedOccupiedVehicle(localPlayer)
    if not theVehicle then -- If the player is not in a vehicle.
      outputChatBox("You must be in a vehicle to toggle the trunk!", 255, 0, 0)
      return
    end
    
    local newState = (getVehicleDoorState(theVehicle, 1) == 0) and 1 or 0 -- If the trunk is closed, set it to 1 (open) otherwise it must be open, so close it. (state 1)
    setVehicleDoorState(theVehicle, 1, newState) -- Set the trunk state for this client.
    triggerServerEvent("syncVehicleTrunkChange", theVehicle, newState) -- Notify the server to set the newState for every player.
end)

-- This function is called whenever another player toggles a vehicle trunk, it executes the function on this client to update the trunk state
-- to the same as what the source player set it to.
function updateDoorStateFromOtherPlayer(newState)
	setVehicleDoorState(source, 1, newState)
end
addEvent("updateVehicleDoorState", true)
addEventHandler("updateVehicleDoorState", root, updateDoorStateFromOtherPlayer)

 

Server

function handleVehicleDoorSync(newState)
	for i, thePlayer in ipairs(getElementsByType("player") do -- For every online player.
		triggerClientEvent(thePlayer, "updateVehicleDoorState", thePlayer, source, newState) -- Trigger the event to set the vehicle door state to the newState provided.
	end
end
addEvent("syncVehicleTrunkChange", true)
addEventHandler("syncVehicleTrunkChange", root, handleVehicleDoorSync)

 

Alternatively to save yourself this hassle, if the function is shared as such is the case with setVehicleDoorState, then you can just execute it on the server-side in which case it will automatically be synced to every client.

I have used this script and create a resource but the doorstate does not working but when we are not in a vehicle the message will we displayed.

How to fix this?

Link to comment
9 hours ago, Heshan_Shalinda_eUnlock said:

I have used this script and create a resource but the doorstate does not working but when we are not in a vehicle the message will we displayed.

How to fix this?

The code I provided was a rough example on how to handle client-server sync.

If you want to use the code for handling opening and closing a vehicle trunk, you can simply adjust the code below to be called from your GUI rather than from a /trunk command, additionally use getVehicleDoorOpenRatio/setVehicleDoorOpenRatio instead and execute it on the server so it automatically syncs it to every client.

Client

addCommandHandler("trunk", function()
	local theVehicle = getPedOccupiedVehicle(localPlayer)
	if not theVehicle then -- If the player is not in a vehicle.
		outputChatBox("You must be in a vehicle to toggle the trunk!", 255, 0, 0)
		return
	end

	local newState = (getVehicleDoorOpenRatio(theVehicle, 1) == 0) and 1 or 0 -- If the trunk ratio is 0, it is closed so we need to open it (value of 1), otherwise we need to close it (value of 0)
	triggerServerEvent("syncVehicleTrunkChange", theVehicle, newState) -- Notify the server to set the new door ratio.
end)

Server

function handleVehicleDoorSync(newState) setVehicleDoorOpenRatio(source, 1, newState) end
addEvent("syncVehicleTrunkChange", true)
addEventHandler("syncVehicleTrunkChange", root, handleVehicleDoorSync)
Edited by Skully
Link to comment
8 hours ago, Skully said:

The code I provided was a rough example on how to handle client-server sync.

If you want to use the code for handling opening and closing a vehicle trunk, you can simply adjust the code below to be called from your GUI rather than from a /trunk command, additionally use getVehicleDoorOpenRatio/setVehicleDoorOpenRatio instead and execute it on the server so it automatically syncs it to every client.

Client

addCommandHandler("trunk", function()
	local theVehicle = getPedOccupiedVehicle(localPlayer)
	if not theVehicle then -- If the player is not in a vehicle.
		outputChatBox("You must be in a vehicle to toggle the trunk!", 255, 0, 0)
		return
	end

	local newState = (getVehicleDoorOpenRatio(theVehicle, 1) == 0) and 1 or 0 -- If the trunk ratio is 0, it is closed so we need to open it (value of 1), otherwise we need to close it (value of 0)
	triggerServerEvent("syncVehicleTrunkChange", theVehicle, newState) -- Notify the server to set the new door ratio.
end)

Server

function handleVehicleDoorSync(newState) setVehicleDoorOpenRatio(source, 1, newState) end
addEvent("syncVehicleTrunkChange", true)
addEventHandler("syncVehicleTrunkChange", root, handleVehicleDoorSync)

I got this but in this you are triggering a server event on client side but I want to trigger a client Event from server side as the reciprocal of this.

I want to show window open/close state other players in the server.

Tell me what are the failures in my script because when I use this it only shows yo me.

-- Client Side --
local function open_event(wndid)
    local playerVehicle = getPedOccupiedVehicle ( source )
    if ( playerVehicle ) then
        if setVehicleWindowOpen( playerVehicle, wndid, not isVehicleWindowOpen( playerVehicle, wndid ) ) then
            outputChatBox( "Driver Window Switched" )
        else
            outputChatBox( "You don't have window!" )
        end
    end
end
local function open_request()
    triggerServerEvent("onVehicleWindowOpenRequest", localPlayer, seatWindows[0]);
end
addCommandHandler("open", open_request)
addEvent("open", true)
addEventHandler( "open", localPlayer, open_event )
addEventHandler ( "onClientGUIClick", GUIEditor.button[13], open_request, false )
-- Server Side --
addEvent("onVehicleWindowOpenRequest", true);
addEventHandler("onVehicleWindowOpenRequest", root,
	function(wndid)
		triggerClientEvent("open", client, wndid);
	end
);

I don't understand what's wrong in this script could you please tell me what's wrong in this script.

Thank you.................

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