Jump to content

Trigger server-side events from client GUI's


MTA.Castiel

Recommended Posts

Hi, there seems to be a problem triggering the server event from the client side (gui button).

- "Bad source element @'triggerServerEvent' [element is clientside]"

I previously had it set "triggerServerEvent("toggleEngine", localPlayer )" but it does nothing, what's the problem?

-- Client
local theButton = guiCreateButton(100, 200, 80, 40, "Toggle Engine On/Off", false)
function handleButton (button,state)
	if ( button == "left" and state == "up" ) then
		if ( source == theButton ) then
			triggerServerEvent("toggleEngine", source )
			outputDebugString ( tostring ( source ) .. " clicked." )
		end
	end
end
addEventHandler("onClientGUIClick", theButton, handleButton)

-- Server
function switchEngine ( playerSource )
	local theVehicle = getPedOccupiedVehicle ( playerSource )
	if theVehicle and getVehicleController ( theVehicle ) == playerSource then
	local state = getVehicleEngineState ( theVehicle )
	setVehicleEngineState ( theVehicle, not state )
    end
end
addEvent("toggleEngine", true)
addCommandHandler ("engine", switchEngine )

 

Link to comment
-- Client
local theButton = guiCreateButton(100, 200, 80, 40, "Toggle Engine On/Off", false)
function handleButton (button,state)
	if ( button == "left" and state == "up" ) then
		if ( source == theButton ) then
			triggerServerEvent("toggleEngine", resourceRoot )
			outputDebugString ( tostring ( source ) .. " clicked." )
		end
	end
end
addEventHandler("onClientGUIClick", theButton, handleButton)

-- Server
function switchEngine ()
	local theVehicle = getPedOccupiedVehicle ( client )
	if theVehicle and getVehicleController ( theVehicle ) == client then
	local state = getVehicleEngineState ( theVehicle )
	setVehicleEngineState ( theVehicle, not state )
    end
end
addEvent("toggleEngine", true)
addEventHandler ("toggleEngine", resourceRoot, switchEngine)
addCommandHandler ("engine", switchEngine )

 

  • Thanks 1
Link to comment
6 hours ago, AngelAlpha said:
-- Client
local theButton = guiCreateButton(100, 200, 80, 40, "Toggle Engine On/Off", false)
function handleButton (button,state)
	if ( button == "left" and state == "up" ) then
		if ( source == theButton ) then
			triggerServerEvent("toggleEngine", resourceRoot )
			outputDebugString ( tostring ( source ) .. " clicked." )
		end
	end
end
addEventHandler("onClientGUIClick", theButton, handleButton)

-- Server
function switchEngine ()
	local theVehicle = getPedOccupiedVehicle ( client )
	if theVehicle and getVehicleController ( theVehicle ) == client then
	local state = getVehicleEngineState ( theVehicle )
	setVehicleEngineState ( theVehicle, not state )
    end
end
addEvent("toggleEngine", true)
addEventHandler ("toggleEngine", resourceRoot, switchEngine)
addCommandHandler ("engine", switchEngine )

 

You should have also explained where he went wrong.

@mta.castiel you forgot to make an event handler.

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