I’m trying to make an object that disables items and deals periodic damage when a Start part is touched, and re-enabling the items and stopping the periodic damage when a Stop part is touched. However, when the effect is re-enabled, previously has touched a Start and Stop part, and then touches the Start part again, the humanoid:TakeDamage() occurs 2-3 times in a row. I know that my code is a bit messy, but I’ve added debounces to everything so I don’t understand why it does this.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local db = false
script.Parent.Start.Touched:Connect(function(hit)
local hum = hit.Parent
if hum and db == false then
db = true
script.Parent.Enabled.Value = true
end
end)
for _, d in pairs(script.Parent.Stop:GetChildren()) do
if d:IsA("BasePart") then
d.Touched:Connect(function(hit)
local hum = hit.Parent
if hum and db == true then
db = false
script.Parent.Enabled.Value = false
end
end)
end
end
local h = player.Character.Humanoid
if script.Parent.Enabled.Value then
local EQ = player.Character:GetChildren()
for _, v in pairs(EQ) do
if v:IsA("Tool") and v.Name ~= "Noclip" and v.Name ~= "Heal" then
print(v.Name .. "1")
v.Parent = player.Backpack
end
end
end
local playerInv = player.Backpack:GetChildren()
script.Parent.Enabled.Changed:Connect(function(changed)
print("changed")
for _, v in pairs (playerInv) do
if v:IsA("Tool") and v.Name ~= "Noclip" and v.Name ~= "Heal" then
local toolScripts = v:GetDescendants()
print(v.Name)
for _, b in pairs(toolScripts) do
if b:IsA("Script") or v:IsA("LocalScript") then
b.Disabled = true
end
end
if v:IsA("Tool") and script.Parent.Enabled.Value == false then
for _, b in pairs(playerInv) do
if b:IsA("Tool") then
local toolScripts = v:GetDescendants()
print(v.Name)
for _, e in pairs(toolScripts) do
if e:IsA("Script") or v:IsA("LocalScript") then
e.Disabled = false
end
end
end
end
end
end
end
local ddb = false
while true do
wait(0.1)
if script.Parent.Enabled.Value and ddb == false then
wait(5)
ddb = true
h.Health -=10
wait(0.1)
ddb = false
end
end
end)
end)
end)
Above is the hiearchy for the object. Server script is used in order to avoid clashing with other game scripts