I have a script that when you touch a part, you should get slow and get damaged, but it is running four times. I have no clue why because I have a debounce. This is the script:
local db = false
script.Parent.Touched:Connect(function(otherpart)
if otherpart.Parent:FindFirstChild("Humanoid") and db == false then
db = true
otherpart.Parent.Humanoid:TakeDamage(10)
otherpart.Parent.Humanoid = otherpart.Parent.Humanoid - 15
wait(1)
otherpart.Parent.Humanoid = otherpart.Parent.Humanoid + 15
db = false
end
end)
Anyways, you seem to be subtracting the humanoid itself, and not the walkspeed. I’ve fixed it in this version:
local db = false
script.Parent.Touched:Connect(function(otherpart)
local humanoid = otherpart.Parent:FindFirstChild("Humanoid")
if humanoid and not db then
db = true
humanoid:TakeDamage(10)
humanoid.WalkSpeed -= 15
task.wait(1)
humanoid.WalkSpeed += 15
db = false
end
end)
One connection to one event will run once per event. Four connections to the same event will run once per event, but add up to four in total.
For example, this will run the same function four times:
local function onHit(hit)
print("hit: "..hit.Name)
end
brick.Touched:Connect(onHit)
brick.Touched:Connect(onHit)
brick.Touched:Connect(onHit)
brick.Touched:Connect(onHit)
script.Parent.Touched:Connect(function(otherpart)
local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
local humanoid = otherpart.Parent:FindFirstChild("Humanoid")
if player and humanoid then
if not player:GetAttribute(“speedDB”) then
player:SetAttribute(“speedDB”, true)
humanoid:TakeDamage(10)
humanoid.WalkSpeed -= 15
task.wait(1)
humanoid.WalkSpeed += 15
player:SetAttribute(“speedDB”, false)
end
end)
script.Parent.Touched:Connect(function(otherpart)
local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
local humanoid = otherpart.Parent:FindFirstChild("Humanoid")
if player and humanoid then
if not player:GetAttribute(“speedDB”) then
player:SetAttribute(“speedDB”, true)
local originalSpeed = humanoid.WalkSpeed
humanoid:TakeDamage(10)
humanoid.WalkSpeed -= 15
task.wait(1)
humanoid.WalkSpeed = originalSpeed
player:SetAttribute(“speedDB”, false)
end
end)
Can we see the full script, or the script that you’re using to connect events? Are you modifying db anywhere else? Maybe a place file as well?
I’m still fairly certain you’re connecting an event four times, either through a loop or through the creation of multiple scripts. You should find the source of the problem first, as that’ll cause a greater performance issue in the future. Using UhTrue’s script will work right now, but you’ll be evading the real problem.