Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my touch damage script not work with debounce?

p = script.Parent --Part in workspace
db = false
p.Touched:Connect(function(hit)
	hum = hit.Parent:FindFirstChild('Humanoid')
	if db == false then
		debounce = true 
		hum.Health = hum.Health - 10
		wait(2)
		debounce = false
	end
end)

It also for some reason gives me a error 22:15:57.427 - Workspace.Part.Script:7: attempt to index global 'hum' (a nil value)

2 answers

its because your never actually checking if the humanoid exists, only the debounce. making a variable that verifies a humanoid exists doesnt do anything. :FindFirstChild() returns nil if the instance is not found, use a if statement

local part = script.Parent 
local db = false

p.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild('Humanoid')

    if db == false and hum then
        debounce = true 
        hum.Health = hum.Health - 10
        wait(2)
        debounce = false
    end
end)

######use local variables

4 comments

0

local variables make absolutely no difference from standard variables

0

i've done a bunch of tests on local variables and they are exactly the same

0

No. They are not. Usage of global variables is bad practice; it makes your code messy.

0

Local variables are also only slightly faster for each access than a global.

You have not checked for a Humanoid. For this you need to use :FindFirstChild("NAME OF OBJECT") if not found th

You can try this script: (if not found Humanoid at hit.Parent. try to find with hit.Parent.Parent)

local p = script.Parent --Part in workspace
local db = false
p.Touched:Connect(function(hit)
    if db == false and hit.Parent:FindFirstChild("Humanoid") then
	local hum = hit.Parent.Humanoid
        debounce = true 
        hum.Health = hum.Health - 10
        wait(2)
        debounce = false
    elseif db == false and hit.Parent.Parent:FindFirstChild("Humanoid") then
	local hum = hit.Parent.Parent.Humanoid
	debounce = true 
        hum.Health = hum.Health - 10
        wait(2)
        debounce = false
    end
end)

Any errors? tell-me on comments.

This discussion is preserved as it was. New answers, comments, and votes are closed.