Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Regenerating parts once every 15 seconds?

I am not skilled enough to do something like this so I used someone's free model. This script regenerates parts but there is 2 things I don't like about it. One is that when you touch the block, it regenerates the part every 0.1 second until you get off it. I put a wait before "regenerate()" and it was regenerating parts every 10 seconds which is what I want. However, if the player touches the block 5 times and walks away, it'll regenerate the parts every 10 seconds until it regenerates 5 times because that's how many times the player touched it. How can I make this that once the player touches the block it regenerates all parts and then waits 10 seconds before the player can touch the block again to regenerate?

model = script.Parent.Parent

backup = model:clone()
enabled = true

function regenerate()
	model:remove()
	model = backup:clone()
	model.Parent = game.Workspace
	model:makeJoints()
	script.Disabled = true
	wait(10)
	script.Disabled = false
end

function onHit(hit)
	if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and enabled then
		regenerate()
	end
end

script.Parent.Touched:connect(onHit)

2 answers

✓ Accepted answer

You disabled the script, and then attempted to re-enable it in the same script. This wouldn't work because the script would be disabled, so it couldn't re-enable itself since it's disabled. Instead use a debounce. Also, I made the variables local and improved some stuff. Try this:

local model = script.Parent.Parent

local backup = model:Clone()
local enabled = true

local debounce = false

local function regenerate()
	if not debounce  then 
		debounce = true
		model:Destroy()
		model = backup:Clone()
		model.Parent = workspace
		model:MakeJoints()
		wait(10)
		debounce = false
	end
end

local function onHit(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid")  then
		regenerate()
	end
end

script.Parent.Touched:Connect(onHit)

This discussion is preserved as it was. New answers, comments, and votes are closed.