Jump to content

[HELP] GUI triggering server event


Galactix

Recommended Posts

Posted

Hello, I'm trying to get my GUI to trigger a server event but I can't manage to find how to send the actual player element needed by the function to work.

--HEROIN DRUG EFFECT ON
function heroinDrugOn(player)
	local oldHeroinCount = getElementData ( player, "Heroin" )
	local heroinStatus = getElementData (player, "heroinStatus")
		if heroinStatus ~= true then
					if oldHeroinCount ~= false then
						setElementData(player, "heroinStatus", true)
						setElementData ( player, "Heroin", oldHeroinCount-1  )
						setPedStat ( player, 24, 1000)
						setElementHealth(player, 200)
						setTimer(heroinDrugOff, 60000, 1, player)
						triggerClientEvent ( player, "heroinOnMessage", root)
					else
					    triggerClientEvent ( player, "heroinNotEnough", root)
						return
					end
		else
			triggerClientEvent (player, "heroinCooldownMessage", root)
		end
	
end
addCommandHandler("useheroin", heroinDrugOn)
addEvent("useHeroin", true)
addEventHandler("useHeroin", root, heroinDrugOn)

And I use:

triggerServerEvent ( "useHeroin", localPlayer)

 

Posted
3 hours ago, Mr.Loki said:

You set the localPlayer as the source of the event.

Yes, that's pretty obvious thanks, but then what do I put instead?

  • Discord Moderators
Posted

Hello.
In MTA you have some predefined variables, but for security reasons, if you want to apply something to the player who triggered the event(actually, to that client) then just use the variable 'client'.
These are variables that are 'hiddenly' defined, but you can use them.

To fix your problem just remove the 'player' argument from the function, and replace every other 'player' argument to 'client'
And use debugscript :)

Posted

Thanks Pirulax, but now it says at line 6 that I am trying to compare a boolean with a number, so why isn't the oldHeroinCount variable acquired properly?

Posted

That's because the elementData for "Heroin" is a number and you are checking if it's true or false.

You should just use:

if oldHeroinCount > 0 then

 

Posted
7 hours ago, Mr.Loki said:

That's because the elementData for "Heroin" is a number and you are checking if it's true or false.

You should just use:


if oldHeroinCount > 0 then

 

Yeah, I managed to solve that myself but thanks

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