Douks08fr Posted December 21, 2010 Share Posted December 21, 2010 Hey everybody, I've got a zombie MTA server running with Shop script (https://community.multitheftauto.com/index.php?p= ... ils&id=720), this script allow players to buy cars/weapons/skills. With ~80-100 players per day, if they all use this script and buy a car, it will be a very big mess ... So, I need your help about destroyElement. I would like to remove the cars who players bought when they die/leave the serv/buy another one. But I don't find how to choose a car created with this script... Thanks a lot Link to comment
Aibo Posted December 21, 2010 Share Posted December 21, 2010 edit this script to add a vehicle owner/creator to a vehicle using setElementData(). and onPlayerQuit/onPlayerWasted/buying another vehicle check vehicles and destroy the ones created by player. or add a vehicle table and put vehicle elements there using player's name as a key, for example. Link to comment
Douks08fr Posted December 21, 2010 Author Share Posted December 21, 2010 edit this script to add a vehicle owner/creator to a vehicle using setElementData().and onPlayerQuit/onPlayerWasted/buying another vehicle check vehicles and destroy the ones created by player. or add a vehicle table and put vehicle elements there using player's name as a key, for example. Thanks for your help About the function who will destroy the car : destroyElement(vehicle) How can I retrieve the vehicle to destroy ? If I use "getElementData ( source, "veh" )", what should I store in "veh" key and what should I use with destroyElmnt ? Thanks Link to comment
12p Posted December 21, 2010 Share Posted December 21, 2010 local vehicles = getElementsByType("vehicle") for i,v in ipairs (vehicles) do --Anything you want here, for each vehicle (it must be "v") end Link to comment
Douks08fr Posted December 21, 2010 Author Share Posted December 21, 2010 local vehicles = getElementsByType("vehicle") for i,v in ipairs (vehicles) do --Anything you want here, for each vehicle (it must be "v") end Thanks. So, could this one work : addCommandHandler ("destroy", function(thePlayer,command) local vehicles = getElementsByType("vehicle") for i,v in ipairs (vehicles) do destroyElement(v) end end end) ? Link to comment
12p Posted December 21, 2010 Share Posted December 21, 2010 yes, but will destroy every car, even the player owned... Use getElementData getVehicleOccupant Link to comment
Discord Moderators Zango Posted December 21, 2010 Discord Moderators Share Posted December 21, 2010 That example deletes all cars on the server, prolly now what you want How can I retrieve the vehicle to destroy ? If I use "getElementData ( source, "veh" )", what should I store in "veh" key and what should I use with destroyElmnt ? You need to store the vehicle element function deletePlayerCar (player) local player = player or source local veh = getElementData (player, 'veh') -- get the vehicle element for the player stored under 'veh' if (getElementType (veh) == 'vehicle') then -- if it's a vehicle destroyElement (veh) -- destroy it end end addEventHandler ('onPlayerQuit', root, deletePlayerCar) -- when he leaves -- ... so when player buys a new car: call function deletePlayerCar before he buys new one Link to comment
Aibo Posted December 21, 2010 Share Posted December 21, 2010 as i recall, you can't store elements in element data Link to comment
Discord Moderators Zango Posted December 21, 2010 Discord Moderators Share Posted December 21, 2010 I bet you're right use a dynamic array instead local playerData = {} function getPlayerVehicle (player) if playerData[player] then return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error else return nil end end function createPlayerData (player) if playerData[player] then return end playerData[player] = {} end function destroyVehicle (player) if playerData[player] then if playerData[player].vehicle then destroyElement (playerData[player].vehicle) playerData[player].vehicle = nil else return false end end end where player gets his vehicle, do this: destroyVehicle (player) playerData[player].vehicle = createVehicle (...) and call createPlayerData when he enters shop or similar Link to comment
Remp Posted December 21, 2010 Share Posted December 21, 2010 of course you can use elements as element data. the only elements that won't work are things like client-side elements (GUI, etc) but even then, its fine as long as synchronize == false https://wiki.multitheftauto.com/wiki/Element_data Link to comment
Douks08fr Posted December 21, 2010 Author Share Posted December 21, 2010 Wow, thats harder than i thought ... I've made something wrong but ... what. The cars are spawned but the older isn't destroyed. local playerData = {} function getPlayerVehicle (player) if playerData[player] then return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error else return nil end end function createPlayerData (player) if playerData[player] then return end playerData[player] = {} end function destroyVehicle (player) if playerData[player] then if playerData[player].vehicle then destroyElement (playerData[player].vehicle) playerData[player].vehicle = nil else return false end end end addEvent ("carBuy", true) addEventHandler ("carBuy", getRootElement(), function(id2, cost2, name2,x, y, z, sRz, thePlayer) if (getPlayerMoney (source) >= tonumber(cost2)) then outputChatBox ("Achat de " .. name2, source, 255, 0, 0, false) outputChatBox ("Prix: " .. cost2, source, 255, 0, 0, false) takePlayerMoney (source, tonumber (cost2)) local x,y,z = getElementPosition (source) local vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) destroyVehicle (player) playerData[thePlayer].vehicle = createVehicle (vehicle) warpPedIntoVehicle(source, vehicle) local blii = createBlipAttachedTo ( vehicle, 56, source ) setElementVisibleTo ( blii, getRootElement ( ), false ) setElementVisibleTo ( blii, source, true ) else outputChatBox ("Vous ne pouvez acheter ce produit [pas assez d'argent]", source, 255, 0, 0, false) end end) Link to comment
12p Posted December 21, 2010 Share Posted December 21, 2010 Check BLACK local playerData = {} function getPlayerVehicle (player) if playerData[player] then return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error else return nil end end function createPlayerData (player) if playerData[player] then return end playerData[player] = {} end function destroyVehicle (player) if playerData[player] then if playerData[player].vehicle then destroyElement (playerData[player].vehicle) playerData[player].vehicle = nil else return false end end end addEvent ("carBuy", true) addEventHandler ("carBuy", getRootElement(), function(id2, cost2, name2,x, y, z, sRz, thePlayer) if (getPlayerMoney (source) >= tonumber(cost2)) then outputChatBox ("Achat de " .. name2, source, 255, 0, 0, false) outputChatBox ("Prix: " .. cost2, source, 255, 0, 0, false) takePlayerMoney (source, tonumber (cost2)) local x,y,z = getElementPosition (source) local vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) [b]destroyVehicle (thePlayer)[/b] --"player" doesn't exist here, I replace it for "thePlayer" playerData[thePlayer].vehicle = createVehicle (vehicle) warpPedIntoVehicle(source, vehicle) local blii = createBlipAttachedTo ( vehicle, 56, source ) setElementVisibleTo ( blii, getRootElement ( ), false ) setElementVisibleTo ( blii, source, true ) else outputChatBox ("Vous ne pouvez acheter ce produit [pas assez d'argent]", source, 255, 0, 0, false) end end) Link to comment
Aibo Posted December 21, 2010 Share Posted December 21, 2010 Wow, thats harder than i thought ... I've made something wrong but ... what. The cars are spawned but the older isn't destroyed. local playerData = {} function getPlayerVehicle (player) if playerData[player] then return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error else return nil end end function createPlayerData (player) if playerData[player] then return end playerData[player] = {} end function destroyVehicle (player) if playerData[player] then if playerData[player].vehicle then destroyElement (playerData[player].vehicle) playerData[player].vehicle = nil else return false end else createPlayerData(player) end end addEvent ("carBuy", true) addEventHandler ("carBuy", getRootElement(), function(id2, cost2, name2,x, y, z, sRz, thePlayer) if (getPlayerMoney (source) >= tonumber(cost2)) then outputChatBox ("Achat de " .. name2, source, 255, 0, 0, false) outputChatBox ("Prix: " .. cost2, source, 255, 0, 0, false) takePlayerMoney (source, tonumber (cost2)) local x,y,z = getElementPosition (source) -- local vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) destroyVehicle(source) playerData[source].vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) warpPedIntoVehicle(source, playerData[source].vehicle) local blii = createBlipAttachedTo ( playerData[source].vehicle, 56, source ) setElementVisibleTo ( blii, getRootElement ( ), false ) setElementVisibleTo ( blii, source, true ) else outputChatBox ("Vous ne pouvez acheter ce produit [pas assez d'argent]", source, 255, 0, 0, false) end end) PS: thePlayer doesnt exist there too (oops, maybe it is, but then what's the "source" for?). Link to comment
Douks08fr Posted December 21, 2010 Author Share Posted December 21, 2010 :o :o That was just beacause I used "player" ? You're really awesome guys, it works 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