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

Is it just me or is humanOther:TakeDamage(5) not working?

So I've been making a game and I chose to make a sword. This is the script

local tool = script.Parent

local function onTouch(partOther)
	
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")
	
	if not humanOther then return end
	
	if humanOther.Parent == tool then return end
	
	humanOther:TakeDamage(5)
end

local function slash()
	
	local str  = Instance.new("StringValue")
	str.Name = "toolanim"
	str.Value = "Slash"
	str.Parent = tool
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch) 

The Script kinda works but it doesn't do 5 damage, it just oneshot kills the other person. How do i fix this?

1 answer

The reason it one shot kills the other person is because when touched, the event not only fires once, but rather fires multiple times in a row. There is a solution to this problem with the use of debounce.

local tool = script.Parent

local debounce = false

local function onTouch(partOther)

    local humanOther = partOther.Parent:FindFirstChild("Humanoid")

    if not humanOther then return end

    if humanOther.Parent == tool then return end

    if humanOther and not debounce then
	debounce = true
	humanOther:TakeDamage(5)
	wait(0.5) --This is adjustable
	debounce = false
    end
end

local function slash()

    local str  = Instance.new("StringValue")
    str.Name = "toolanim"
    str.Value = "Slash"
    str.Parent = tool
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch) 

The debounce acts kind of like a cooldown, after it detects a collision through the touch event, it will wait the determined time until the event can be fired again.

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