mint3d Posted February 23, 2014 Share 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 ) Link to comment
pa3ck Posted February 23, 2014 Share 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. Link to comment
mint3d Posted February 23, 2014 Author Share Posted February 23, 2014 So what function could I use I am sh*t with mysql Link to comment
tosfera Posted February 23, 2014 Share Posted February 23, 2014 Use the mysql select function to retrieve the data. Link to comment
mint3d Posted February 23, 2014 Author Share Posted February 23, 2014 any help as i said i am bad with mysql Link to comment
mint3d Posted February 24, 2014 Author Share Posted February 24, 2014 Nope I made it for vG scripts Link to comment
pa3ck Posted February 24, 2014 Share 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. Link to comment
mint3d Posted February 24, 2014 Author Share 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? Link to comment
Moderators Citizen Posted February 24, 2014 Moderators Share 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). Link to comment
mint3d Posted February 24, 2014 Author Share 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 ) Link to comment
pa3ck Posted February 24, 2014 Share 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 Link to comment
mint3d Posted February 24, 2014 Author Share 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? Link to comment
pa3ck Posted February 24, 2014 Share 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. Link to comment
mint3d Posted February 24, 2014 Author Share 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 ) Link to comment
pa3ck Posted February 24, 2014 Share 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. Link to comment
Moderators Citizen Posted February 24, 2014 Moderators Share 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) 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