How can I improve this train regeneration system?

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)

image

Any and all help is very much appreciated!

1 Like

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,

local back = save:clone()

Make another clone? Can’t you do?

local back = regen:Clone()
1 Like

Oh gosh I remember making a similar post to this, here’s what I would do:

  • Create a collision part that detects if the train model has hit the part

    • If it has, then the Regen Button will wait 15 seconds to work again
  • Inside the script is a table that will check, insert, and remove any names that are called: Train

    • The table will first use table.find to check if there’s a train model
    • If there isn’t, then it will use table.insert to add it to the TrainList table
    • After collision with the part, it will use table.remove to remove the specific Train

Reference Post that made me internally die
https://devforum.roblox.com/t/regen-script-whilst-still-keeping-the-original-model/1115474/112

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.