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

.Touched event firing off too many times and not at the right time?

I'm making a weapon that, when it hits an object with a humanoid in it, will deal a set amount of damage. The weapon treats the "hitbox" on it as always being active and in the Output, will show that it has hit many times, up to thousands of times in a second. Is there a way to fix this?

TL;DR: I'm trying to fix the collisions on my weapon so that it doesn't instakill whatever it touches.

Here is my code.

local function onTouched(hit)
	if active == false then return end
	
	local human = hit.Parent:findFirstChild("Humanoid")
	if (human ~= nil) then
		human.Health = human.Health - hurt.Value
		print "touching a humanoid"
		wait(1)
	end
end

local function damage(player,boxactive,aerial,swing1,swing2)
	if boxactive == false then
		print "box not active"
		return end
	print "box active"
	if aerial == true then
		hurt.Value = 10
		print ("aerial damage set "..hurt.Value)
	elseif swing1 == true then
		hurt.Value = 20
		print ("swing1 damage set "..hurt.Value)
	elseif swing2 == true then
		hurt.Value = 15
		print ("swing2 damage set "..hurt.Value)
	end
	
	while boxactive == true do
		active = boxactive	
		script.Parent.Touched:Connect(onTouched)
		wait(1)
	end
end

boxActiveEvent.OnServerEvent:Connect(damage)

Thank you in advance.

2 comments

0

Instead of using human.Health = human.Health - hurt.Value You should use human:TakeDamage.

0

add a debounce canDamage = true when you hit do canDamage = false wait(idk how long)

2 answers

Ok So first you are using human.Health = human.Health - hurt.Value You should use human:TakeDamage and you should also have a wait at the end

local function onTouched(hit)
    if active == false then return end

    local human = hit.Parent:findFirstChild("Humanoid")
    if (human ~= nil) then
        human:TakeDamage(hurt.Value)
        print "touching a humanoid"
    end
	wait(1)
end

local function damage(player,boxactive,aerial,swing1,swing2)
    if boxactive == false then
        print "box not active"
        return end
    print "box active"
    if aerial == true then
        hurt.Value = 10
        print ("aerial damage set "..hurt.Value)
    elseif swing1 == true then
        hurt.Value = 20
        print ("swing1 damage set "..hurt.Value)
    elseif swing2 == true then
        hurt.Value = 15
        print ("swing2 damage set "..hurt.Value)
    end

    while boxactive == true do
        active = boxactive  
        script.Parent.Touched:Connect(onTouched)
    end
    wait(1)
end

boxActiveEvent.OnServerEvent:Connect(damage)

Hope this helps

1 comment

0

It still connects way too many times, but thank you.

local Touched = false -- Added

function onTouched(hit)
	 
	local human = hit.Parent:FindFirstChild("Humanoid")
	
	if human then
		if Touched == false then -- Changed
			
			Touched = true
			
	        human:TakeDamage(hurt.Value) -- Changed
	        print "touching a humanoid"
	
	        wait(1)
			Touched = true
	
		end
    end
end
	 
function damage(player,boxactive,aerial,swing1,swing2)
	
	 if boxactive == false then -- Changed
	        
		print "box not active"
		return
		
	elseif boxactive == true then
		
	    print "box active"
	
	end
	
    if aerial == true then
	
        hurt.Value = 10
        print ("aerial damage set "..hurt.Value)

    elseif swing1 == true then
	
        hurt.Value = 20
        print ("swing1 damage set "..hurt.Value)

    elseif swing2 == true then
	
        hurt.Value = 15
        print ("swing2 damage set "..hurt.Value)

    end

    if boxactive == true then -- Added

	while true do
        	active = boxactive 
        	wait(1)
		-- Deleted "script.Parent.Touched:Connect(onTouched)", You shouldnt add this in "While Loop", I think thats whats causing it hit up to thousand times per second(it might break ur script too)
	 end
	
	 script.Parent.Touched:Connect(onTouched) -- Changed

    end


end
	
boxActiveEvent.OnServerEvent:Connect(damage)

Im not sure about some stuff as this is not the full script, but u can try it. U can also change some lines as i changed the script without knowing what is ur full script.

If theres any error, pls comment below.

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