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)
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)
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.