I currently have a basic regeneration system with a cooldown that respawns a train. However, despite the cooldown, this can easily be abused to create lag and possibly crash a server if a user were to spam the button enough. I am looking for a way to improve this system so that a train cannot be regenerated if there is already one in its place. The script and explorer layout are below, keep in mind that TrainDebris is a folder used to group together regenerated models to prevent Workspace clutter.
location = game.Workspace.TrainDebris
regen = script.Parent.Parent.Cart
save = regen:clone()
function onClicked(Clicker)
if script.CanGenerate.Value == true then
script.CanGenerate.Value = false
local back = save:clone()
back.Parent = location
back:MakeJoints()
print("--Begin--")
print("Regen button activated.")
print("Cart ID:")
print(script.Parent.Parent.Name)
print("Player Name:")
print(Clicker.Name)
print("--End--")
wait(15)
script.CanGenerate.Value = true
elseif script.CanGenerate.Value == false then
print("--Begin--")
print("Possible regen spam detected.")
print("Player Name:")
print(Clicker.Name)
print("--End--")
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
For checking if a train is in its location you could possibly use FindPartsInRegion3 and check if one of the parts found is a parent of the Cart, and if it is, don’t spawn till it goes away.
Also to help with lag, you can basically get rid of the other check if CanGenerate is false since it’s printing 5 times and printing is already a bit straining, so combine that with an autoclicker and it’s going to cause issues.
Also another thing, why are you making a clone of the cart and then using that to clone the cart again? You can’t do it with one clone or? Basically, here
regen = script.Parent.Parent.Cart
save = regen:clone()
You’re cloning the cart and then using that clone to,