Rotti Posted May 5, 2013 Share Posted May 5, 2013 Hello, I want to make a script that can set an owner for a vehicle and only the owner can lock or unlock his car. Here is what happens when someone buys a car: function Perennial_buy () local Perennial = createVehicle(404, -2600, 2268, -- s8) --> setElementData(Perennial, "save", "save")--for the save script takePlayerMoney(source, 2000) --take money setTimer(warpPedIntoVehicle, 50, 1, source, Perennial)--warp player in vehicle setElementData(Faggio, "owner", getPlayerName(source))--set the owner outputServerLog("[AUTOHÄNDLER] "..getPlayerName(source).." kaufte einen Perennial")--server log end I don't know what that text in line 2 is but it is not in the script just in the forum and i can't delete it... and thats the script for the lock function lockcar(player) posX, posY, posZ = getElementPosition(player) local lockSphere = createColSphere( posX, posY, posZ, 20 ) local locker = getPlayerName( player ) local nearbyVehicles = getElementsWithinColShape( lockSphere, "vehicle" ) destroyElement( lockSphere ) for index, nearbyVehicle in ipairs( nearbyVehicles ) do if getElementData(nearbyVehicle, "owner") == getPlayerName(source) then setVehicleLocked(nearbyVehicle, true) end end end function binds() bindKey(source, "L", "down", lockcar) end addEventHandler("onPlayerLogin", getRootElement(), binds) But it doesn't locks the car! I think it has to be something with the owner script because an outputChatBox before the "for" works! please help Link to comment
PaiN^ Posted May 5, 2013 Share Posted May 5, 2013 function lockcar ( player ) local posX, posY, posZ = getElementPosition ( player ) local lockSphere = createColSphere ( posX, posY, posZ, 20 ) local locker = getPlayerName ( player ) local nearbyVehicles = getElementsWithinColShape ( lockSphere, 'vehicle' ) destroyElement ( lockSphere ) for index,nearbyVehicle in ipairs( nearbyVehicles ) do if getElementData ( nearbyVehicle, 'owner') == getPlayerName ( player ) then setVehicleLocked ( nearbyVehicle, true ) end end end function binds() bindKey ( source, 'L', 'down', lockcar ) end addEventHandler ( 'onPlayerLogin', root, binds ) * Note : Using serial instead of name is much better. Because unlike serials, names are changable . Link to comment
Rotti Posted May 5, 2013 Author Share Posted May 5, 2013 I've found one mistake: at setElementData in the buy script the but it didn't help so far... But I've corrected it Link to comment
PaiN^ Posted May 5, 2013 Share Posted May 5, 2013 I've found one mistake:at setElementData in the buy script the but it didn't help so far... But I've corrected it I don't get it Did it work or you still have problems ? Link to comment
PaiN^ Posted May 5, 2013 Share Posted May 5, 2013 Tell us what are they and give us the part ( from your code ) where you have problems . Link to comment
Rotti Posted May 5, 2013 Author Share Posted May 5, 2013 Its still the same problem. The vehicle doesn't locks when i press L. And i think it has to be something with the Owner, because the Vehicle locks if i take out this part: if getElementData(nearbyVehicle, "owner") == getPlayerName(player) setVehicleLocked(nearbyVehicle, true) end Link to comment
PaiN^ Posted May 5, 2013 Share Posted May 5, 2013 Post the code when someone buys a car in pastebin website . Link to comment
Rotti Posted May 5, 2013 Author Share Posted May 5, 2013 I have already posted the code at the top Link to comment
PaiN^ Posted May 5, 2013 Share Posted May 5, 2013 I know. There is a forum bug so the code is not showing correctly ! function Perennial_buy () local Perennial = createVehicle(404, -2600, 2268, -- s8) --> setElementData(Perennial, "save", "save")--for the save script takePlayerMoney(source, 2000) --take money setTimer(warpPedIntoVehicle, 50, 1, source, Perennial)--warp player in vehicle setElementData(, "owner", getPlayerName(source))--set the owner outputServerLog("[AUTOHÄNDLER] "..getPlayerName(source).." kaufte einen Perennial")--server log end The bug is in line 2 as you can see . And the error is in setElementData at line 6 . Link to comment
Rotti Posted May 5, 2013 Author Share Posted May 5, 2013 do you mean this text with img src and so on? thats only on this website not in the script and i can't delete it... Link to comment
PaiN^ Posted May 5, 2013 Share Posted May 5, 2013 I know, It's a bug in the forum . + As i said : the error is in setElementData at line 6 Link to comment
Rotti Posted May 5, 2013 Author Share Posted May 5, 2013 Can you say exactly what the error is? I have already fixed the in the setElementData Link to comment
Moderators IIYAMA Posted May 6, 2013 Moderators Share Posted May 6, 2013 You should not store the player name or the serial, just use the player userdata. Elements can't have the same userdata..... -_-" Link to comment
Rotti Posted May 6, 2013 Author Share Posted May 6, 2013 Ok can you explain how to get the userdata? ive nver worked with user data Link to comment
Moderators IIYAMA Posted May 6, 2013 Moderators Share Posted May 6, 2013 (edited) You already have the user data and that is the player. local vehicle = createVehicle ( 270, 0, 0, 0) -- create a vehicle, when you create an element it will returns a userdata. outputChatBox(tostring(vehicle)) A player does also have a user data. addCommandHandler ( "myUserData", function ( player) outputChatBox(tostring(player)) end) Edited May 6, 2013 by Guest Link to comment
Rotti Posted May 6, 2013 Author Share Posted May 6, 2013 so simply setElementData(Perennial, "owner", player) ??? Link to comment
Moderators IIYAMA Posted May 6, 2013 Moderators Share Posted May 6, 2013 yes, see my sample. (tostring is making sure that when I put it the chatbox, it won't give any errors. Userdata have to be a string to output.) function Perennial_buy () local Perennial = createVehicle(404, -2600, 2268, -- s8) --> setElementData(Perennial, "save", "save")--for the save script takePlayerMoney(source, 2000) --take money setTimer(warpPedIntoVehicle, 50, 1, source, Perennial)--warp player in vehicle setElementData(source, "owner", source)--set the owner outputServerLog("[AUTOHÄNDLER] "..getPlayerName(source).." kaufte einen Perennial")--server log end function lockcar ( player ) local posX, posY, posZ = getElementPosition ( player ) local lockSphere = createColSphere ( posX, posY, posZ, 20 ) for index,nearbyVehicle in pairs(getElementsWithinColShape ( lockSphere, 'vehicle' )) do -- just use pairs instead of ipairs if getElementData ( nearbyVehicle, 'owner') == player then setVehicleLocked ( nearbyVehicle, true ) end end destroyElement ( lockSphere ) end addEventHandler ( 'onPlayerLogin', root, function () bindKey ( source, 'L', 'down', lockcar ) end) BTW to many element data will start lagging, it is recommended to learn working with tables. Like: vehicleOwnGroup = {} vehicleOwnGroup[player]= { , , , } Why is element data laggy? Because when you create it it will be send to all the players in the game. (only client element data have an option that disallow the server to share it.) Link to comment
Rotti Posted May 6, 2013 Author Share Posted May 6, 2013 Thank you for your help it worked but if i reconnect i can't lock the vehicle again... So if the vehicle is unlocked, i reconnect and try to lock it it doesn't locks I hope that you understand it Link to comment
Moderators IIYAMA Posted May 6, 2013 Moderators Share Posted May 6, 2013 yes, that is true, it is because when you reconnect your userdata got changed. To safe this per-ma, you have to use account data(mysql lite) or mysql. Account data is easier, my mysql gives more performance at bigger data bases, but is harder to script. https://wiki.multitheftauto.com/wiki/SetAccountData https://wiki.multitheftauto.com/wiki/GetAccountData 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