mint3d Posted February 23, 2014 Posted February 23, 2014 Ok so I made this script but every time I restart it it doesn't load from the mysql I need help to make it load its all server sided mysql = exports.mysql function powerChange ( thePlayer, command, power ) if exports.global:isPlayerLeadAdmin(thePlayer) then power = tonumber ( power ) local veh = getPedOccupiedVehicle ( thePlayer ) if power and veh then local success = setVehicleHandling ( veh, "engineAcceleration", power ) if success then outputChatBox ( "Your vehicle's acceleration has been changed to: "..power.." ", thePlayer, 0, 255, 0 ) local dbid = getElementData(veh, "dbid") mysql:query_free("UPDATE vehicles SET power="..power.." WHERE id="..dbid) exports.logs:dbLog(thePlayer, 40, veh, "SETPOWER: "..power.."") else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end elseif not veh then outputChatBox ( "You're not in a vehicle.", thePlayer, 255, 0, 0 ) elseif not power then outputChatBox ( "Syntax: /setpower [1 - 100000]", thePlayer, 255, 0, 0 ) end end end addCommandHandler ( "setpower", powerChange )
pa3ck Posted February 23, 2014 Posted February 23, 2014 You are only saving the 'power', you never retreive it from SQL, obviously, it won't load if you never wrote a function for it.
mint3d Posted February 23, 2014 Author Posted February 23, 2014 So what function could I use I am sh*t with mysql
tosfera Posted February 23, 2014 Posted February 23, 2014 Use the mysql select function to retrieve the data.
mint3d Posted February 23, 2014 Author Posted February 23, 2014 any help as i said i am bad with mysql
pa3ck Posted February 24, 2014 Posted February 24, 2014 It's pretty straight forward. You need a function to retreive the handlings. Use 'SELECT' instead of 'INSERT' @ MySQL query, then set the handlings. It's nearly the same thing you've just done, if you could get this far, you will surely be able to do it.
mint3d Posted February 24, 2014 Author Posted February 24, 2014 mysql = exports.mysql function loadpower ( thePlayer, power ) if exports.global:isPlayerLeadAdmin(thePlayer) then power = tonumber ( power ) local veh = getPedOccupiedVehicle ( thePlayer ) if power and veh then local success = setVehicleHandling ( veh, "engineAcceleration", power ) if success then local dbid = getElementData(veh, "dbid") mysql:query_free("SELECT vehicles power="..power.." WHERE id="..dbid) exports.logs:dbLog(thePlayer, 40, veh, "SELECTPOWER: "..power.."") else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end addEventHandler ( "onResourceStart", getRootElement(), loadpower ) Is this right?
Moderators Citizen Posted February 24, 2014 Moderators Posted February 24, 2014 mysql = exports.mysql function loadpower ( thePlayer, power ) if exports.global:isPlayerLeadAdmin(thePlayer) then power = tonumber ( power ) local veh = getPedOccupiedVehicle ( thePlayer ) if power and veh then local success = setVehicleHandling ( veh, "engineAcceleration", power ) if success then local dbid = getElementData(veh, "dbid") mysql:query_free("SELECT vehicles power="..power.." WHERE id="..dbid) exports.logs:dbLog(thePlayer, 40, veh, "SELECTPOWER: "..power.."") else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end addEventHandler ( "onResourceStart", getRootElement(), loadpower ) Is this right? No. You have to get the data from the database (using the a query) and store the result in power (so remove it from your function arguments and the query should executed before the setVehicleHandling You also have to add the argument veh to know which vehicle you want to load the power of. local vehID = getElementID(veh) local power = mysql:query_free("SELECT power INTO vehicles WHERE id="..vehID) So you shall not call this function with the onResourceStart event but right after the vehicle has been created (in the function that loads all vehicles from the database).
mint3d Posted February 24, 2014 Author Posted February 24, 2014 Like this? mysql = exports.mysql function loadpower ( thePlayer, power ) if exports.global:isPlayerLeadAdmin(thePlayer) then local vehID = getElementID(veh) local power = mysql:query_free("SELECT power INTO vehicles WHERE id="..vehID) power = tonumber ( power ) local veh = getPedOccupiedVehicle ( thePlayer ) if power and veh then local success = setVehicleHandling ( veh, "engineAcceleration", power ) if success then exports.logs:dbLog(thePlayer, 40, veh, "SELECTPOWER: "..power.."") else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end addEventHandler ( "onResourceStart", getRootElement(), loadpower )
pa3ck Posted February 24, 2014 Posted February 24, 2014 mysql = exports.mysql function loadpower ( thePlayer ) if exports.global:isPlayerLeadAdmin(thePlayer) then local veh = getPedOccupiedVehicle ( thePlayer ) local vehID = getElementData(veh, "dbid") local power = mysql:query_free("SELECT `power` FROM vehicles WHERE id="..vehID) power = tonumber ( power ) if power and veh then local success = setVehicleHandling ( veh, "engineAcceleration", power ) if success then exports.logs:dbLog(thePlayer, 40, veh, "SELECTPOWER: "..power.."") else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end end end addCommandHandler( "test", loadpower ) -- If you use 'onResourceStart' then load and set the handlings for every vehicle with a loop
mint3d Posted February 24, 2014 Author Posted February 24, 2014 I have a question when I start the res I will need to do /test [VEHID] to load it? or will I just do /test and all will load?
pa3ck Posted February 24, 2014 Posted February 24, 2014 If you are in a vehicle and type /test it will load the power ( if it's in the SQL ) and set its handling. Go ahead and modify the script to load all of the vehicles, give it a try.
mint3d Posted February 24, 2014 Author Posted February 24, 2014 I am bad at mysql but you would need to remove locals I think mysql = exports.mysql function loadpower ( thePlayer ) if exports.global:isPlayerLeadAdmin(thePlayer) then veh = getPedOccupiedVehicle ( thePlayer ) vehID = getElementData(veh, "dbid") local power = mysql:query_free("SELECT `power` FROM vehicles WHERE id="..vehID) power = tonumber ( power ) if power and veh then success = setVehicleHandling ( veh, "engineAcceleration", power ) if success then exports.logs:dbLog(thePlayer, 40, veh, "SELECTPOWER: "..power.."") else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end else outputChatBox ( "Setting power failed.", thePlayer, 255, 0, 0 ) end end end addEventHandler ( "onResourceStart", getRootElement(), loadpower )
pa3ck Posted February 24, 2014 Posted February 24, 2014 Remove local? Why would you need to remove it? That's nothing to do with MySQL... I suggest you start with easy scripts, don't go in to SQL if you don't know what's local for. Go step-by-step, don't rush it.
Moderators Citizen Posted February 24, 2014 Moderators Posted February 24, 2014 I'm agree with pa3ck. the local keyword has nothing to do with mysql nor MTA. It's from the Lua language (Lua ~= MTA). By the way, we need the code that loads the vehicles from database. This is where you have to get and set the power of the vehicle or call the loadPower(veh)
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