kieran Posted January 16, 2018 Share Posted January 16, 2018 (edited) I have a client side GUI with 2 buttons, if the player clicks yes, the local player should be carried to the server side script as "hitElement" I am doing this because "source" does nothing, and if I use getLocalPlayer() as an argument for the triggerEvent it returns an error saying it got a userdata value. -->[[Client]]<-- local marker = createMarker( 0,0,0,"cylinder", 2, 0, 200, 55, 255 ) --Marker function GUIwindow() local screenW, screenH = guiGetScreenSize() window = guiCreateWindow((screenW - 312) / 2, (screenH - 104) / 2, 312, 104, "", false)--GUI guiWindowSetSizable(window, false) showCursor(true) label = guiCreateLabel(56, 30, 201, 17, "Do you want to spawn a truck?", false, window) yes_btn = guiCreateButton(56, 64, 89, 30, "Yes", false, window) --Button no_btn = guiCreateButton(168, 65, 89, 29, "No", false, window) addEventHandler("onClientGUIClick", yes_btn, triggerEvent) --Handler end addEventHandler("onMarkerHit", marker, GUIwindow) function triggerEvent(button, state) if source == yes_btn and button == "left" and state == "up" then triggerServerEvent("spawnTruck", localPlayer, getLocalPlayer()) --Trying to carry local player as hitElement to server destroyElement(window) showCursor(false) end end -->[[Server]]<-- function spawnTheTruck(hitElement)--If player choses yes in client side GUI, then they will get a truck if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then if isPedOnGround ( hitElement ) then --If so, check if player is on foot local Truck = createVehicle (515, 0, 5, 5, 0, 0, 0) warpPedIntoVehicle (hitElement, Truck) end end end addEvent("spawnTruck", true) addEventHandler("spawnTruck", getRootElement(), spawnTheTruck) --Server side event Info of why the triggerEvent isn't working would be nice, thanks for help. Edited January 16, 2018 by kieran Link to comment
KaMi Posted January 16, 2018 Share Posted January 16, 2018 (edited) try deleting the "getLocalPlayer". If that don´t change nothing, try sending the buttons. Edited January 16, 2018 by <~KaMiKaZe~> Link to comment
kieran Posted January 16, 2018 Author Share Posted January 16, 2018 (edited) 8 minutes ago, <~kamikaze~> said: try deleting the "getLocalPlayer" I have tried this, nothing happens, when I look at triggerServerEvent it's event, element, [arguments...] So my best guess is for hitElement (on custom event) to get the player from client side when they click a button, I need to pass localPlayer (which I have tried as well as "getLocalPlayer()" and "client") to the server as an argument, not just as an element... So it'd be this: triggerServerEvent("spawnTruck", localPlayer, localPlayer) Which doesn't work... Edited January 16, 2018 by kieran Link to comment
KaMi Posted January 16, 2018 Share Posted January 16, 2018 10 minutes ago, <~KaMiKaZe~> said: If that don´t change nothing, try sending the buttons. Link to comment
kieran Posted January 16, 2018 Author Share Posted January 16, 2018 @<~KaMiKaZe~> what do you mean send the buttons? Do you mean passing localPlayer on the handler for buttons? Because that doesn't work, handlers can't take arguments like triggerEvent Link to comment
KaMi Posted January 16, 2018 Share Posted January 16, 2018 -----------------CLIENT local marker = createMarker( 0, 0, 0,"cylinder", 2, 0, 200, 55, 255 ) --Marker function GUIwindow() local screenW, screenH = guiGetScreenSize() window = guiCreateWindow((screenW - 312) / 2, (screenH - 104) / 2, 312, 104, "", false)--GUI guiWindowSetSizable(window, false) showCursor(true) label = guiCreateLabel(56, 30, 201, 17, "Do you want to spawn a truck?", false, window) yesbtn = guiCreateButton(56, 64, 89, 30, "Yes", false, window) --Button nobtn = guiCreateButton(168, 65, 89, 29, "No", false, window) addEventHandler("onClientGUIClick", yesbtn, triggerEvent) --Handler end addEventHandler("onClientMarkerHit", marker, GUIwindow) function triggerEvent(button, state, hitElement) if source == yesbtn then if not isPedInVehicle ( localPlayer ) then triggerServerEvent("spawnTruck", localPlayer, hitElement) --Trying to carry local player as hitElement to server guiSetVisible(window, false) showCursor(false) else end end end -----------------------SERVER addEvent("spawnTruck", true) function spawnTheTruck(hitElement)--If player choses yes in client side GUI, then they will get a truck local Truck = createVehicle (515, 0, 5, 5, 0, 0, 0) warpPedIntoVehicle (source, Truck) end addEventHandler("spawnTruck", getRootElement(), spawnTheTruck) The Triggerserverevent isn´t the error. Your error it was put "onMarkerHit" (serverside event) in a clientside, put "destroyelement" (you don´t need that because you can use the function "guiSetVisible" and, you don´t need "hitElement", "source" going to be detected like player because you are triggering in clientside ( for default, "player" going to be "source" if you are triggering ). Link to comment
Enargy, Posted January 16, 2018 Share Posted January 16, 2018 You are putting triggerEvent as a name while also is a lua function, change it. At the attached function's eventHandler add a new argument setting False. Link to comment
kieran Posted January 16, 2018 Author Share Posted January 16, 2018 2 hours ago, Enargy, said: You are putting triggerEvent as a name while also is a lua function, change it. At the attached function's eventHandler add a new argument setting False. Both good points, but this was example code I typed quickly to show how I was trying to pass the PLAYER as an argument to server side, I will put actual code below, just made example because it's easier to read... In the script the marker is server side, as well as the vehicle spawning, change teams, and checks for element data. Server side is just GUI, sorry for the confusion with the bad example. Client Only need first 2 functions, third just shows you what I want to do if they click no. function spawnTheTruck() local screenW, screenH = guiGetScreenSize() window = guiCreateWindow((screenW - 312) / 2, (screenH - 104) / 2, 312, 104, "Spawn a truck or use your own?", false) guiWindowSetSizable(window, false) showCursor(true) label1 = guiCreateLabel(56, 27, 201, 17, "Yes - Spawn a truck.", false, window) label2 = guiCreateLabel(56, 40, 201, 17, "No - I brought my own.", false, window) guiLabelSetHorizontalAlign ( label1, "center") guiLabelSetHorizontalAlign ( label2, "center") Yes_btn = guiCreateButton(56, 64, 89, 30, "Yes", false, window) No_btn = guiCreateButton(168, 65, 89, 29, "No", false, window) addEventHandler("onClientGUIClick", Yes_btn, spawnYes) addEventHandler("onClientGUIClick", No_btn, spawnNo) end addEvent("spawnTruck", true) addEventHandler("spawnTruck", getRootElement(), spawnTheTruck) function spawnYes(button, state, hitElement) if source == Yes_btn and button == "left" and state == "up" then triggerServerEvent("spawnTruckYes", localPlayer, hitElement) destroyElement(window) showCursor(false) end end function spawnNo(button, state) if source == No_btn and button == "left" and state == "up" then --triggerServerEvent("spawnTruckNo", localPlayer, this) destroyElement(window) showCursor(false) end end Server Only need first 3 functions, last one is just there to show how markers are made. local markerStore = {} --Stores markers function TruckerStart ( hitElement, matchingDimension ) if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then if not (isGuestAccount (getPlayerAccount (hitElement))) then local account = getPlayerAccount (hitElement) if (account) then local PlayerShipments = getAccountData( account, "Trucker.pres" ) if (PlayerShipments) then setElementData (hitElement, "Trucker.pres", PlayerShipments) else setElementData (hitElement, "Trucker.pres", 0) end triggerClientEvent ( hitElement, "spawnTruck", hitElement) end end end end function spawnNo(hitElement) --If player choses no in client side GUI, then they won't get a truck if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then if ( TruckerTeam ) then if isPedOnGround ( hitElement ) then local playerTeam = getPlayerTeam ( hitElement ) if not ( playerTeam == TruckerTeam) then setPlayerTeam(hitElement, TruckerTeam) end --[[triggerClientEvent ( hitElement, "destroyIron", hitElement)<<Custom handler for iron miner script]] triggerClientEvent ( hitElement, "shipmentMarker", hitElement) else outputChatBox("You must be on foot!", hitElement, 255, 0, 0) end end end end addEvent("spawnTruckNo", true) addEventHandler("spawnTruckNo", getRootElement(), spawnNo) function spawnYes(hitElement)--If player choses yes in client side GUI, then they will get a truck if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then if ( TruckerTeam ) then if isPedOnGround ( hitElement ) then local playerTeam = getPlayerTeam ( hitElement ) if not ( playerTeam == TruckerTeam) then setPlayerTeam(hitElement, TruckerTeam) end local spawnedTruck = getElementData( hitElement, "Trucker.truck" ) --Check if player has spawned a truck if ( spawnedTruck ~= false ) then destroyElement(spawnedTruck) --If they have, destroy the truck spawnedTruck = nil setElementData(hitElement, "Trucker.truck", false) --And set there data to false else triggerClientEvent ( hitElement, "destroyIron", hitElement) triggerClientEvent ( hitElement, "shipmentMarker", hitElement) local x, y, z, rx, ry, rz, name local marker_data = markerStore[source] if marker_data then x,y,z,rx,ry,rz,name = marker_data[4],marker_data[5],marker_data[6],marker_data[7],marker_data[8],marker_data[9],marker_data[10] local Truck = createVehicle (515, x, y, z+1, rx, ry, rz) setElementData(hitElement, "Trucker.truck", Truck) warpPedIntoVehicle (hitElement, Truck) end end else outputChatBox("You must be on foot!", hitElement, 255, 0, 0) end end end end addEvent("spawnTruckYes", true) addEventHandler("spawnTruckYes", getRootElement(), spawnYes) function createMarkers() for i=1,#TruckerMarker do local x,y,z = TruckerMarker[i][1],TruckerMarker[i][2],TruckerMarker[i][3] local marker = createMarker( x,y,z,"cylinder", 1, 0, 200, 55, 255 ) markerStore[marker] = TruckerMarker[i] TruckerJobBlip = createBlipAttachedTo ( marker, 42, 2, 0, 0, 0, 0, 0, 500 ) addEventHandler("onMarkerHit", marker, TruckerStart) end end createMarkers() Hope this helps you understand what I'm trying to accomplish... Link to comment
3aGl3 Posted January 17, 2018 Share Posted January 17, 2018 (edited) No no, you don't send the localClient to the Server. This is handled by MTA internally and will be available in the event Handler on the server side as the client variable. Sending the client to the server would work but it's pointless as you have the client variable and it's also unsafe as the elements send can be faked. See this thread for further predefined variables, also take a look at the triggerServerEvent entry in the wiki... Edited January 17, 2018 by 3aGl3 Link to comment
Storm-Hanma Posted January 17, 2018 Share Posted January 17, 2018 Trigger client or server event only works you cant use trigger event Link to comment
ShayF2 Posted January 17, 2018 Share Posted January 17, 2018 triggerServerEvent('eventName',root,localPlayer) Do not use other functions to get root or get local player, just use this, and then on the event handler you'll have something like addEventHandler('eventName',root,function(player) end) -- where player would be your local player. 1 Link to comment
Addlibs Posted January 17, 2018 Share Posted January 17, 2018 In the server-side function spawnTheTruck, you're testing whether matchingDimension is true but it is undefined therefore a nil value, a false. The first if-scope evaluation returns false and the body of the if does not get executed. 1 Link to comment
kieran Posted January 17, 2018 Author Share Posted January 17, 2018 (edited) 1 hour ago, MrTasty said: In the server-side function spawnTheTruck, you're testing whether matchingDimension is true but it is undefined therefore a nil value, a false. The first if-scope evaluation returns false and the body of the if does not get executed. Thanks, works great, but there's a slight problem with how I'm doing this (this is the last thing that needs fixed ) If you look at line 46 you'll see I set marker_data to be source, as it was originally spawning when you hit the marker and I'm remodeling it for GUI spawning, the reason I use marker_data is because it got the marker I was currently in, do you know of any other ways to get the marker other than using onMarkerHit handler and getting table index? local TruckerTeam = createTeam("Trucker", 20, 100, 150) TruckerMarker = {--marker [x,y,z] truck spawn point: [x,y,z,rotationx,rotationy,rotationz], [location] {2179.841796875, -2263.6940917969, 13.8, 2167.4401855469, -2273.9362792969, 14 ,-0, 0, 220, "Ocean Docks"}, {-1737.9622802734, 20.285757064819, 2.7, -1710, 10, 4, 0, 0, 315, "Easter Basin"}, {1643, 2354, 10, 1634, 2365, 11, -0, 0, 90, "Redsands West"} }--Above table is for the job markers, player walks into one, they become trucker and a truck is spawned. local markerStore = {} --Stores markers function TruckerStart ( hitElement, matchingDimension ) if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then if not (isGuestAccount (getPlayerAccount (hitElement))) then local account = getPlayerAccount (hitElement) if (account) then local PlayerShipments = getAccountData( account, "Trucker.pres" ) if (PlayerShipments) then setElementData (hitElement, "Trucker.pres", PlayerShipments) else setElementData (hitElement, "Trucker.pres", 0) end triggerClientEvent ( hitElement, "spawnTruck", hitElement) end end end end addEvent("spawnTruckYes", true) addEventHandler("spawnTruckYes", root, function (hitElement) if isElement(hitElement) and getElementType(hitElement) == "player" then if ( TruckerTeam ) then if isPedOnGround ( hitElement ) then local playerTeam = getPlayerTeam ( hitElement ) if not ( playerTeam == TruckerTeam) then setPlayerTeam(hitElement, TruckerTeam) end local spawnedTruck = getElementData( hitElement, "Trucker.truck" ) --Check if they have spawned a truck if ( spawnedTruck ~= false ) then destroyElement(spawnedTruck) --If they have, destroy the truck spawnedTruck = nil setElementData(hitElement, "Trucker.truck", false) --Set there data to false else triggerClientEvent ( hitElement, "shipmentMarker", hitElement) local x, y, z, rx, ry, rz, name local marker_data = markerStore[source] --Getting the marker data if marker_data then outputDebugString("Marker data: "..marker_data) x,y,z,rx,ry,rz,name = marker_data[4],marker_data[5],marker_data[6],marker_data[7],marker_data[8],marker_data[9],marker_data[10] local Truck = createVehicle (515, x, y, z+1, rx, ry, rz) setElementData(hitElement, "Trucker.truck", Truck) warpPedIntoVehicle (hitElement, Truck) end end else outputChatBox("You must be on foot!", hitElement, 255, 0, 0) end end end end ) function createMarkers() for i=1,#TruckerMarker do local x,y,z = TruckerMarker[i][1],TruckerMarker[i][2],TruckerMarker[i][3]--x = key 1, y = key 2, z = key 3 local marker = createMarker( x,y,z,"cylinder", 1, 0, 200, 55, 255 ) markerStore[marker] = TruckerMarker[i] TruckerJobBlip = createBlipAttachedTo ( marker, 42, 2, 0, 0, 0, 0, 0, 500 ) addEventHandler("onMarkerHit", marker, TruckerStart) end end createMarkers() Can't thank you guys enough for the help. Edited January 17, 2018 by kieran Link to comment
raynner Posted January 17, 2018 Share Posted January 17, 2018 triggerServerEvent(string "EventName", element "localPlayer", arguments getLocalPlayer(), value 1, ...) --Server Recevid -- Event Name = Event -- LocalPlayer = source -- Arguments = localPlayer -- value = 1 addEvent("EventName", true) function Test(player, value) --[[ note: player = getLocalPlayer, source = localPlayer, value = value(1) !!! --]] end addEventHandler("EventName", getRootElement(), Test) triggerServerEvent("event", raiz(source), arguments) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now