Hello guys, I am trying to make a spawner button but I need something that will stop it from spamming spawn and spawning 100000 vehicles and crashing the server. How could I achieve this?
script.Parent.MouseButton1Click:connect(function(GetCar)
Mod = game.ServerStorage.Quad --Change test to whatever you have named the item you want to spawn
clone = Mod:clone()
clone.Parent = workspace
clone:MakeJoints()
end)
local CanSpawn = true -- Spawn
local SpawnCooldown = 5 -- Cooldown
script.Parent.MouseButton1Click:Connect(function(GetCar) -- Function to actually spawn the car
if CanSpawn then -- If CanSpawn is true then
CanSpawn = false -- Car can no longer spawn
Mod = game:GetService("ServerStorage").Quad -- The quad
clone = Mod:Clone() -- Mod clones
clone.Parent = workspace -- Mod's repositioned to workspace
clone:MakeJoints() -- Mod makes welds
corountine.wrap(function() --Function that doesnt slow down the script
wait(SpawnCooldown) -- Waiting for the cooldown
CanSpawn = true -- Car can spawn again
end)() -- End to corountine.wrap [the "()" is importnant!]
end -- End to the [if CanSpawn then]
end) -- End to the "script.Parent.MouseButton1Click:Connect(function(Getcar)"
Thankfully I was able to fix it/write a new code.
I am just having a problem now with the color as you can see, I added a line
so that each time the bike spawns, they spawn in a different color each time.
The thing is, the color does change but it changes once per server. So if I run the game the bike will change color but each time it will be the same color, I have to stop the game and run it again for it to change to another color but it still stays the same so i am very confused.
if carModel then
local clone = carModel:Clone()
clone.Parent = game.Workspace
clone.Body.Quad.Color.Color = Color3.new(x, y, z)
clone:MakeJoints()
end
local randomColorX
local randonColorY
local randomColorZ
if carModel then
randomColorX = math.random(1,100)
randonColorY = math.random(1,100)
randomColorZ = math.random(1,100)
local clone = carModel:Clone()
clone.Parent = game.Workspace
clone.Body.Quad.Color.Color = Color3.new(randomColorX , randonColorY, randomColorZ )
clone:MakeJoints()
end
i think? [change the numbers because i do not know how “Color3.new” works, only RGB
[ik that RGB has max limit for 255, dont know for color3.new
local Seat = Quad.Seat -- Change it to the seat part
local TimeLimit = 60 -- 1 minute
local StartTimer = Instance.new("BoolValue", Seat)
StartTimer.Name = "StartTimer"
StartTimer.Value = false
local ActuallyAtimer = TimeLimit
Seat.Changed:Connect(function(str)
if str == "Occupant" then -- if it doesnt detects Occupant then remove this line
if Seat.Occupant == nil then
StartTimer = true
elseif Seat.Occupant ~= nil then
StartTimer = false
end
end -- This line belongs to "if str == "Occupant" then
end)
StartTimer.Changed:Connect(function(val)
if StartTimer.Value == true then
repeat wait(1) ActuallyAtimer =- 1
until
ActuallyAtimer = 0 or StartTimer == false
if ActuallyAtimer == 0 then
Quad:Destroy()
elseif ActuallyAtimer ~= 0 and StartTimer == false then
ActuallyAtimer = TimeLimit
end
end
end)