I wanted to spawn a boat in my game when you press a button because anyone can press this button and this button is on a wall in game you could spawn the boat 1000 times or more. I wanted to prevent this and for this I wrote a script that doesn’t quite work. Actually, but it does not quite work to the end and I come there unfortunately no further. Therefore I need from you a few suggestions. Thank you!
local Bkd = game.Workspace:WaitForChild("Boat")
local Dipi = game.Workspace.Parel.ManuScript
if Bkd then
Dipi.Parent = game.SoundService
wait(5)
Bkd:Destroy()
wait(5)
game.SoundService.ManuScript.Parent = game.Workspace.Parel
end
Try keeping a list of people and what they have spawned in. Then if they already have something spawned in you can delete that vehicle and spawn in the new one. Then set the new one as the currently spawned in item. I don’t know what the Dipi variable is so I’m not going to use it.
Also, I assume your using a ClickDetector | Roblox Creator Documentation for the button.
local Players = game:GetService("Players")
local Boat = workspace:WaitForChild("Boat")
local Profiles = {}
Players.PlayerAdded:Connect(function(player)
Profiles[player] = false
end
Players.PlayerRemoving:Connect(function(player)
if Profiles[player] then
Profiles[player]:Destroy()
end
Profiles[player] = nil
end
-- whenever button pressed do:
Button.ClickDetector.MouseClick:Connect(function(player)
if Profile[player] then
Profile[player]:Destroy()
end
local boat = Boat:Clone()
-- The code you use to position the boat can go here
boat.Parent = workspace
Profile[player] = boat
end
Some coding suggestions I have so you can improve are to try making your code more readable as well as learning how Roblox’s API works and it’s syntax. Here’s some resources to help you learn what I covered in the code I provided: