Having Problems with making damaging part

I am having problems with this script it kills the player instantly

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then
		local anotherdb = false
		if anotherdb ~= true then
			anotherdb = true
			humanoid.Health = humanoid.Health - 10
			wait(1)
			anotherdb = false
		end
	end
end)

The debounce is not working properly, move the variable outside the function scope/environment.

1 Like

you mean i make a bool value on that part?

That’s exactly what I meant, move the variable referencing to the bool outside the scope to the global scope. This will cause the variable to not be a new variable each time it’s fired.

This is actually not working again debounce problem
I made BoolValue and the value is false

This is the code

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then
		local anotherdb = script.Parent.Value.Value
		if anotherdb ~= true then
			anotherdb = true
			humanoid.Health = humanoid.Health - 10
			wait(10)
			anotherdb = false
		end
	end
end)

Thanks

No, that’s not exactly what I meant. A debounce outside the function scope means…

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then
		local anotherdb = false -- MOVING THIS
		if anotherdb ~= true then
			anotherdb = true
			humanoid.Health = humanoid.Health - 10
			wait(1)
			anotherdb = false
		end
	end
end)

…to…

local anotherdb = false -- HERE

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then
		if anotherdb ~= true then
			anotherdb = true
			humanoid.Health = humanoid.Health - 10
			wait(1)
			anotherdb = false
		end
	end
end)

Scopes in programming are different environments, which encapsulates variables in an environment where the global scope cannot access if it’s not in its environment but the environment. This means in the former case scenario, you were trying to declare the variable over and over again, resulting in multiple environments with its own bool.

2 Likes

Thank you very much it actually worked!