Hello, so I made a script where if you touch a part from a folder then it fires a function. For some reason, the touched function keeps firing over and over again, even if the player isn’t touching any of the parts. Another thing is that if I delete most of the contents in the folder the script works just fine, but that by itself isn’t consistent because if I delete all the models and the folder inside the folder then it breaks, but if I delete all the parts and leave the models and folder it still breaks. If I delete most of what’s inside the folder and leave only 5 parts then it works.
Here’s my code:
--script broken rn don't enable
local Lighting = game:GetService("Lighting") --services
local TweenService = game:GetService("TweenService")
local CurrentCheckpoint = script.Parent.CurrentCheckpoint --values
local DefaultCheckpoint = script.Parent.DefaultCheckpoint
local IgnoreArms = script.Parent.IgnoreArms
local ButtonsTable = {} --the buttons table which holds buttons that deactivate
local Player = game.Players.LocalPlayer
local LeftArm = Player.Character:FindFirstChild("Left Arm") --arms
local RightArm = Player.Character:FindFirstChild("Right Arm")
local debounce = true
return function()
local function ClearButtonTable() --clears all buttons from the table
table.clear(ButtonsTable, ButtonsTable)
end
local function CheckpointTouched(checkpoint) --checkpoint script
if Player.Character:FindFirstChild("Humanoid") then
local CheckpointTweenOn = TweenService:Create(checkpoint, TweenInfo.new(3), {Color = Color3.new(0, 1, 0)})
CurrentCheckpoint.Value = checkpoint --sets the current checkpoint to the last checkpoint that got touched
checkpoint.CanTouch = false
checkpoint.Color = Color3.new(1, 1, 0)
for i, v in pairs(script.Parent.Checkpoints:GetChildren()) do
if v ~= checkpoint then
local CheckpointTweenOff = TweenService:Create(v, TweenInfo.new(1), {Color = Color3.new(1, 0, 0)})
if v:FindFirstChild("ParticleEmitter") then
v.ParticleEmitter:Destroy()
end
v.CanTouch = true
CheckpointTweenOff:Play()
end
for j, w in pairs(script.Parent.Parent:GetDescendants()) do
if checkpoint:FindFirstChild("RemoveButtons").Value == true then
if w:IsA("BoolValue") and w.Name == "Pressed" and w.Value == false and w.Parent:FindFirstChild("Timer") then
w.Changed:Connect(function()
if w.Value == true then
table.insert(ButtonsTable, w)
checkpoint.CanTouch = true
else
if w.Parent.Timer.Value < 1 then
checkpoint.CanTouch = false
end
end
end)
end
ClearButtonTable()
end
end
end
CheckpointTweenOn:Play()
script.Pressed:Play()
if not checkpoint:FindFirstChild("ParticleEmitter") then
local Particles = script.ParticleEmitter:Clone()
Particles.Parent = checkpoint
Particles.Enabled = true
wait(2)
Particles.Enabled = false
else
local Particles = checkpoint.ParticleEmitter
Particles.Enabled = true
wait(2)
Particles.Enabled = false
end
end
end
local function DeathBrickTouched(hit, deathbrick) --death brick script
if Player.Character:FindFirstChild("Humanoid") then
local DarknessTween1 = TweenService:Create(Lighting, TweenInfo.new(1), {ExposureCompensation = -20})
local DarknessTween2 = TweenService:Create(Lighting, TweenInfo.new(0.5), {ExposureCompensation = 0})
if debounce ~= false then
if (deathbrick:FindFirstChild("Activated") and not deathbrick.Activated.Value) then
return
end
if IgnoreArms.Value == true and hit == RightArm or IgnoreArms.Value == true and hit == LeftArm then
return
else
if DefaultCheckpoint.Value == nil then
print("No default checkpoint set! Please set one!")
script.Pressed:Play()
else
script.Touched:Play()
Player.Character.HumanoidRootPart.Anchored = true
deathbrick.CanTouch = false
debounce = false
wait(1)
DarknessTween1:Play()
wait(0.5)
if CurrentCheckpoint.Value == nil then
Player.Character.HumanoidRootPart.CFrame = DefaultCheckpoint.Value.CFrame + Vector3.new(0, 4, 0)
else
Player.Character.HumanoidRootPart.CFrame = CurrentCheckpoint.Value.CFrame + Vector3.new(0, 4, 0)
end
DarknessTween2:Play()
Player.Character.HumanoidRootPart.Anchored = false
deathbrick.CanTouch = true
debounce = true
if CurrentCheckpoint.Value ~= nil and CurrentCheckpoint.Value:FindFirstChild("RemoveButtons").Value == true or DefaultCheckpoint.Value:FindFirstChild("RemoveButtons").Value == true then
for i, v in pairs(ButtonsTable) do
v.Value = false
end
end
end
end
end
end
end
for i, v in pairs(script.Parent.Checkpoints:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function()
CheckpointTouched(v)
end)
end
end
for i, v in pairs(script.Parent.DeathBricks:GetDescendants()) do
if v:IsA("BasePart") and v.Name == "Death Brick" or v:FindFirstChild("DeathBrick") then
v.Touched:Connect(function(hit)
DeathBrickTouched(hit, v)
end)
end
end
end
the bottom most part of the script is the problem.
I’ve also attached a screenshot of what’s inside the folder
by the way, the return function() that the script is inside of is just because of how other stuff in the game works, it doesn’t have anything to do with my problem