Hello everyone, I have an issue with a script I have and can’t seem to resolve the issues I currently have.
-
What do I want to Achieve?
-
I would like to achieve creating a working spawner that allows a player to spawn a vehicle, and keep that same vehicle until he/she deletes it.
-
If another player tries to spawn that same vehicle while the other player currently has it spawned in the game, they won’t be able to until the owner of that vehicle deletes it.
-
What is the issue?
-
I have a script that doesn’t seem to work. I’m not sure if it’s even the correct way to script what I need.
-
I’m not an advanced scripter, and not experienced in LUA scripting, so I’m having a difficult time trying to resolve this issue.
-
What Solutions have I Tried so Far?
-
I have tried looking at other topics in the Roblox Developer Forum and can’t seem to find what I’m looking for.
-
I have tested a basic cloning script and tested one that was close to my idea, but both together won’t work.
I appreciate any responses and assistance.
location = game.Workspace
regen = game.ServerStorage.Vehicles.Motorcycle
save = regen:clone()
back = {}
function onClicked(player)
if back[player.Name] ~= nil then
back[player.Name]:Destroy()
end
back[player.Name] = save:Clone()
back[player.Name].Parent = location
back[player.Name]:MakeJoints()
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
This is an extremely simple fix. I can see that this script was taken from the toolbox, so it’s apparent you’ve no idea on how to script. I’d like to direct you to some learning tools on basic Lua so you can start to get an understanding of it better, it’s a very useful thing to know when developing here on Roblox.
With that out of the way, let’s get to the issue.
The script looks for a model upon the regen button being clicked.
if back[player.Name] ~= nil then
back[player.Name]:Destroy()
end
This if-statement detects if the item is in existence (~= means not equal to, nil means non-existent)
Destroy() removes the given item from Workspace and any of that item’s children.
Therefore, removing this section of the script entirely will void the item being deleted when the button is pressed, thus fixing your script.
Edit: Made the block of code “pretty”
2 Likes
Hello, I appreciate you responding to my topic. I will test it as soon as possible and message back here if I have further issues. Thank you!
1 Like
Hello, I deleted if back[player.Name] ~= nil then
back[player.Name]:Destroy()
end from the script and when I try to spawn the vehicle it doesn’t seem to work.
Here, try this code.
local location = game.Workspace
local regen = game.ServerStorage.Vehicles.Motorcycle:Clone()
local back = {}
– remove player
game.Players.PlayerRemoving:Connect(function(plr)
back[plr]:Destroy()
back[plr]= nil
end)
function onClicked(player)
if back[player] == nil then
back[player] = regen:Clone()
back[player].Parent = location
back[player]:MakeJoints()
else
back[player]:Destroy()
back[player] = nil
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)