Karuzo Posted February 20, 2014 Share Posted February 20, 2014 Hey Guys, so i have a problem with my engine-script. If i press 'X' it should show the engine_on.png if the engine is on, and engine_off if the engine is off. But that doesn't really work. Hope you could help me. Code: Server: function motor_func ( player) local veh = getPedOccupiedVehicle(player) local veh1 = getPedOccupiedVehicle(player) if veh then setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) else setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) end end function light_func ( player) local veh = getPedOccupiedVehicle(player) if veh then if getVehicleOverrideLights(veh) ~= 2 then setVehicleOverrideLights(veh,2) else setVehicleOverrideLights(veh,1) end end end addEventHandler("onVehicleEnter",getRootElement(),function ( player,seat) if seat == 0 then outputChatBox("Starte den Motor mit 'X' und die Lichter mit 'L'",player,0,125,0) local enginestate = getElementData(source,"engine") if not enginestate then setElementData(source,"engine",false) triggerClientEvent("OnMotor", source) enginestate = false end if enginestate == false then setVehicleEngineState(source,false) triggerClientEvent("OffMotor", source) end bindKey(player,"l","down",light_func,player) bindKey(player,"x","down",motor_func,player) end end) addEventHandler("onVehicleExit",getRootElement(),function ( player,seat) if seat == 0 then unbindKey(player,"l","down",light_func,player) unbindKey(player,"x","down",motor_func,player) end end) Client: function MotorOn() Motor = guiCreateStaticImage(X1,Y1, Width1, Height1,"data/engine_on", false) end addEvent("OnMotor", true) addEventHandler("OnMotor", root, MotorOn) function MotorOff() guiStaticImageLoadImage(Motor, "data/engine_off") end addEvent("OffMotor", true) addEventHandler("OffMotor", root, MotorOff) Link to comment
Gallardo9944 Posted February 20, 2014 Share Posted February 20, 2014 You might also use the tables for players. Like this way: local states = { } -- YOUR BUTTON PART if states[player] == nil then states[player] = false -- or "true" if you want it to be enabled by default else states[player] = not states[player] end -- SEND CLIENTSIDE But that's just the best way imo. The actual problem might hide clientside. Make sure it triggers the stuff by adding outputChatBoxes to clientside. Also try using DX or anything like that. Also, your images don't have any extension, is that fine? Shouldn't it be some kind of "engine_on.png"? Link to comment
Karuzo Posted February 20, 2014 Author Share Posted February 20, 2014 Also, your images don't have any extension, is that fine? Shouldn't it be some kind of "engine_on.png"? Oh , haha missed that completely. But it still doesn't work if i use local player = source. function motor_func () local player = source local veh = getPedOccupiedVehicle(player) if veh then setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) else setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) end end function light_func () local player = source local veh = getPedOccupiedVehicle(player) if veh then if getVehicleOverrideLights(veh) ~= 2 then setVehicleOverrideLights(veh,2) else setVehicleOverrideLights(veh,1) end end end addEventHandler("onVehicleEnter",getRootElement(),function (seat) local player = source if seat == 0 then outputChatBox("Starte den Motor mit 'X' und die Lichter mit 'L'",player,0,125,0) local enginestate = getElementData(source,"engine") if not enginestate then setElementData(source,"engine",false) triggerClientEvent("OnMotor", player) enginestate = false end if enginestate == false then setVehicleEngineState(source,false) triggerClientEvent("OffMotor", player) end bindKey(player,"l","down",light_func,player) bindKey(player,"x","down",motor_func,player) end end) addEventHandler("onVehicleExit",getRootElement(),function (seat) local player = source if seat == 0 then unbindKey(player,"l","down",light_func,player) unbindKey(player,"x","down",motor_func,player) end end) Link to comment
Wei Posted February 20, 2014 Share Posted February 20, 2014 function motor_func() local veh = getPedOccupiedVehicle(source) if veh then setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) else setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) end end function light_func() local veh = getPedOccupiedVehicle(source) if veh then if getVehicleOverrideLights(veh) ~= 2 then setVehicleOverrideLights(veh,2) else setVehicleOverrideLights(veh,1) end end end addEventHandler("onVehicleEnter",getRootElement(),function (player, seat) if seat == 0 then outputChatBox("Starte den Motor mit 'X' und die Lichter mit 'L'",player,0,125,0) local enginestate = getElementData(source,"engine") if not enginestate then setElementData(source,"engine",false) triggerClientEvent("OnMotor", player) enginestate = false end if enginestate == false then setVehicleEngineState(source,false) triggerClientEvent("OffMotor", player) end bindKey(player,"l","down",light_func) bindKey(player,"x","down",motor_func) end end) addEventHandler("onVehicleExit",getRootElement(),function (player, seat) if seat == 0 then unbindKey(player,"l","down",light_func) unbindKey(player,"x","down",motor_func) end end) Try that EDIT: triggerClientEvent("OnMotor", player) will trigger to show image for all players. you should have it like triggerClientEvent(player,"OnMotor", player) Link to comment
Karuzo Posted February 20, 2014 Author Share Posted February 20, 2014 Thank you Wei, it works, it shows now the Image. But if i wanna turn on the engine it doesn't work. and i get this error : Code: function motor_func() local veh = getPedOccupiedVehicle(source) if veh then setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) else setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine",not getElementData(veh,"engine")) end end function light_func() local veh = getPedOccupiedVehicle(source) if veh then if getVehicleOverrideLights(veh) ~= 2 then setVehicleOverrideLights(veh,2) else setVehicleOverrideLights(veh,1) end end end addEventHandler("onVehicleEnter",getRootElement(),function (player, seat) if seat == 0 then outputChatBox("Starte den Motor mit 'X' und die Lichter mit 'L'",player,0,125,0) local enginestate = getElementData(source,"engine") if not enginestate then setElementData(source,"engine",false) triggerClientEvent(player,"OnMotor", player) enginestate = false end if enginestate == false then setVehicleEngineState(source,false) triggerClientEvent("OffMotor", player) end bindKey(player,"l","down",light_func) bindKey(player,"x","down",motor_func) end end) addEventHandler("onVehicleExit",getRootElement(),function (player, seat) if seat == 0 then unbindKey(player,"l","down",light_func) unbindKey(player,"x","down",motor_func) triggerClientEvent("CloseMotor", player) end end) Link to comment
Wei Posted February 20, 2014 Share Posted February 20, 2014 function motor_func() local veh = getPedOccupiedVehicle(source) if veh then setVehicleEngineState(veh,not getVehicleEngineState(veh)) setElementData(veh,"engine", getVehicleEngineState(veh)) end end Try this, but your code is a mess if not enginestate then setElementData(source,"engine",false) triggerClientEvent(player,"OnMotor", player) enginestate = false end if enginestate == false then setVehicleEngineState(source,false) triggerClientEvent("OffMotor", player) end this will do if engine is off it will turn it off ... Link to comment
Karuzo Posted February 20, 2014 Author Share Posted February 20, 2014 Bad argument @ getPedOccupiedVehicle . Why ? local veh = getPedOccupiedVehicle(source) Link to comment
Wei Posted February 20, 2014 Share Posted February 20, 2014 Edited post like 5 times local enginestate = {} function motor_func(player) local veh = getPedOccupiedVehicle(player) if veh then setVehicleEngineState(veh,not enginestate[veh]) enginestate[veh] = not enginestate[veh] if (enginestate[veh]) then triggerClientEvent("OnMotor", player) else triggerClientEvent("OffMotor", player) end end end function light_func(player) local veh = getPedOccupiedVehicle(player) if veh then if getVehicleOverrideLights(veh) ~= 2 then setVehicleOverrideLights(veh,2) else setVehicleOverrideLights(veh,1) end end end addEventHandler("onVehicleEnter",getRootElement(),function (player, seat) if seat == 0 then outputChatBox("Starte den Motor mit 'X' und die Lichter mit 'L'",player,0,125,0) enginestate[source] = false setVehicleEngineState(source, false) triggerClientEvent(player,"OffMotor", player) bindKey(player,"l","down",light_func, player) bindKey(player,"x","down",motor_func, player) end end) addEventHandler("onVehicleExit",getRootElement(),function (player, seat) if seat == 0 then unbindKey(player,"l","down",light_func) unbindKey(player,"x","down",motor_func) triggerClientEvent(player, "CloseMotor", player) end end) this should work as you want Link to comment
Moderators Citizen Posted February 20, 2014 Moderators Share Posted February 20, 2014 You might also use the tables for players. Like this way: local states = { } -- YOUR BUTTON PART if states[player] == nil then states[player] = false -- or "true" if you want it to be enabled by default else states[player] = not states[player] end -- SEND CLIENTSIDE But that's just the best way imo. The actual problem might hide clientside. Make sure it triggers the stuff by adding outputChatBoxes to clientside. Also try using DX or anything like that. Also, your images don't have any extension, is that fine? Lol why do you need a table ?! Looks like you are coming from SAMP, are you ? @KRZO: First, wtf is that ? local veh = getPedOccupiedVehicle(player) local veh1 = getPedOccupiedVehicle(player) Looks like a random test you did to try fixing this. Please remove the second line. Then if not enginestate then and if enginestate == false then are exactly the same ! And you should use the following "if else" statement, since it's a boolean and that it only can take two value: if enginestate then --means if enginestate == true --instructions to stop the engine else --if not (so if it's equal to false) --instructions to start the engine end You also should take care about what parameters (and default values) a function takes: https://wiki.multitheftauto.com/wiki/TriggerClientEvent By not giving the player as first argument, you will trigger this event for every players ! And you obviously only want to trigger for it for the player who pressed the X key. So please add player before the event name. Now on the client side (everything should be fine server side at this point): I'll first suggest to "sandbox" the Motor global into the current file script. Wtf did you just say ?! Haha don't worry, nothing that complicated. I mean that if you create the Motor variable like this: function myFunc() Motor = guiCreateStaticImage(blahblabla) end function myOtherFunc() --Using Motor variable end It will works yeah, but this variable will be accessible for all others client sided script files (because we are in a client sided script atm) in your resource. So now imagine that another client sided script file is using the same variable name for some reason ? (ie: an object that will represents an engine/motor) ? Yeah, nothing good will happen So we will just prevent globals to be accessible outside of the current file by doing like this: local Motor --Here I'm saying that this Motor variable will be only available in this file. function myFunc() Motor = guiCreateStaticImage(blahblabla) end function myOtherFunc() --Using Motor variable end And here we go, our script is still working and myOtherFunc can still access the Motor variable. Now let's go back to your actual code: function MotorOn() Motor = guiCreateStaticImage(X1,Y1, Width1, Height1,"data/engine_on", false) end addEvent("OnMotor", true) addEventHandler("OnMotor", root, MotorOn) First you need to specify the file extension so "data/engine_on" should have been "data/engine_on.png" (if it's a png file ofc) Then I already can see a problem that might be not obvious for some scripters. Your function is creating a new image when MotorOn() function is called. Ok right but the problem is that this function is called everytime you start the engine, so you will just stack the same images again and again. To fix that, I suggest you to check if Motor is equal to nil (which means that this variable is not seted so that the image isn't created yet). If it is equal to nil, create it, if not, then it means that the image already exists and it's currently the "engine_off.png" image. So in this case, you will change/switch the image back to the "engine_on.png" image: function MotorOn() if Motor == nil then --you can also do: if not Motor then Motor = guiCreateStaticImage(X1,Y1, Width1, Height1,"data/engine_on.png", false) else guiStaticImageLoadImage(Motor, "data/engine_on.png") end end addEvent("OnMotor", true) addEventHandler("OnMotor", root, MotorOn) And finally use the event onClientVehicleExit to hide/delete the image. If you hide the image, you will have to use onClientVehicleEnter to show it again. (Use guiSetVisible ofc) Finally done, hope it will be usefull Link to comment
Karuzo Posted February 21, 2014 Author Share Posted February 21, 2014 Hey Guys, first of all , i just wanted to tell you that i'm so happy that you try to solve my problem ok so, I did what Citizen and Wei said, it works everything, but i have a problem: If i leave the car and enter it , dbgscript 3 gives me an error: My Code: Client: local Motor function MotorOn() if not Motor then Motor = guiCreateStaticImage(X,Y, Width, Height,"data/engine_on.png", false) else guiStaticImageLoadImage(Motor, "data/engine_on.png") end end addEvent("OnMotor", true) addEventHandler("OnMotor", root, MotorOn) function MotorOff() guiStaticImageLoadImage(Motor, "data/engine_off.png") end addEvent("OffMotor", true) addEventHandler("OffMotor", root, MotorOff) Server: local enginestate = {} function motor_func(player) local veh = getPedOccupiedVehicle(player) if veh then setVehicleEngineState(veh,not enginestate[veh]) enginestate[veh] = not enginestate[veh] if (enginestate[veh]) then triggerClientEvent("OnMotor", player) else triggerClientEvent("OffMotor", player) end end end function light_func(player) local veh = getPedOccupiedVehicle(player) if veh then if getVehicleOverrideLights(veh) ~= 2 then setVehicleOverrideLights(veh,2) else setVehicleOverrideLights(veh,1) end end end addEventHandler("onVehicleEnter",getRootElement(),function (player, seat) if seat == 0 then outputChatBox("Starte den Motor mit 'X' und die Lichter mit 'L'",player,0,125,0) enginestate[source] = false setVehicleEngineState(source, false) triggerClientEvent(player,"OffMotor", player) bindKey(player,"l","down",light_func, player) bindKey(player,"x","down",motor_func, player) end end) addEventHandler("onVehicleExit",getRootElement(),function (player, seat) if seat == 0 then unbindKey(player,"l","down",light_func) unbindKey(player,"x","down",motor_func) triggerClientEvent(player, "CloseMotor", player) end end) Link to comment
Wei Posted February 21, 2014 Share Posted February 21, 2014 Motor = guiCreateStaticImage(X,Y, Width, Height,"data/engine_on.png", false) guiSetVisible(Motor, false) function MotorOn() guiSetVisible(Motor, true) guiStaticImageLoadImage(Motor, "data/engine_on.png") end addEvent("OnMotor", true) addEventHandler("OnMotor", root, MotorOn) function MotorOff() guiSetVisible(Motor, true) guiStaticImageLoadImage(Motor, "data/engine_off.png") end addEvent("OffMotor", true) addEventHandler("OffMotor", root, MotorOff) function CloseMotor() guiSetVisible(Motor, false) end addEvent("CloseMotor", true) addEventHandler("CloseMotor", root, CloseMotor) Try that Link to comment
Karuzo Posted February 21, 2014 Author Share Posted February 21, 2014 Thank you, works perfectly 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