Script doesn't work the way i want it to

Hello, devs!
I am currently trying to recreate the checkpoint effect that every difficulty chart obby has.
SCRIPT:

local p = workspace.Folder:GetChildren()
debounce = false

local inf = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out,
	0,
	true,
	0
)
local endinginf = 
	{
		Size = Vector3.new(9, 1, 9);
		Transparency = 0.5;
		Color = Color3.fromRGB(85, 255, 127)
	}


for i, v in pairs(p) do
	v.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local parttween = ts:Create(v, inf, endinginf)

		if debounce == false then
			if humanoid then
				debounce = true
				parttween:Play()
			end
		end
	end)
end

(It’s a local script)
BUG:
robloxapp-20210602-2152169.wmv (1.4 MB)
Basically if one of the folder’s children does the event, the other one doesn’t.

I’m not sure but these are the things I would personally do differently, try this and maybe it helps.

  • put local before the debounce

  • instead of having the “p” variable, put it directly into the for i,v pairs loop

Hope this helps out somehow :upside_down_face:

You’re never setting the debounce to false.

You didn’t set the debounce to false after it runs the tween. Also, keep in mind that tween:Play() does not yield; you will have to add another wait() statement for the debounce to work properly.

I want it to happen one time for each children. (not all of them at the same time)

You set the debounce as false by default at the start of the script, so only 1 Checkpoint would get activated

If you want to trigger for every checkpoint, put the debounce variable inside the loop:

local p = workspace.Folder:GetChildren()

local inf = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out,
	0,
	true,
	0
)
local endinginf = 
	{
		Size = Vector3.new(9, 1, 9);
		Transparency = 0.5;
		Color = Color3.fromRGB(85, 255, 127)
	}


for i, v in pairs(p) do
    local debounce = false
	v.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local parttween = ts:Create(v, inf, endinginf)
        
		if debounce == false then
			if humanoid then
				debounce = true
				parttween:Play()
			end
		end
	end)
end
1 Like

Then create a separate variable for each children. You should probably use attributes for that.

for i, v in pairs(p) do
	v.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local parttween = ts:Create(v, inf, endinginf)

		if not humanoid:GetAttribute('Already Ran') then --by default, they won't have this attribute
			if humanoid then
				humanoid:SetAttribute('Already Ran', true) --once they ran, the attribute will be set for them and they won't be allowed to run anymore
				parttween:Play()
			end
		end
	end)
end

Or you can use a table instead to keep track of each humanoid:

local record = {}

for i, v in pairs(p) do
	v.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local parttween = ts:Create(v, inf, endinginf)

		if not record[humanoid.Parent.Name] then --assuming that the parent of each humanoid has a unique name?
			if humanoid then
				record[humanoid.Parent.Name] = true --records down the humanoid's name so that they won't be allowed to run anymore
				parttween:Play()
			end
		end
	end)
end