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

why does this function run more than once ?

Hello, Full script is below thx already for any help

this script is for a sword so slash refers to the slashanimation, etc

my problem is that when i run the function DoDamage()[LINE 20] on line 49 or 63 the function never stops running, from that point if the sword just touches the dummy without actually doing the slash animation it already takes damage

how do i get the DoDamage() function to stop once it has hit the dummy?

if anything is unclear just comment thank you for the help

FULL SCRIPT:

script.Parent.Parent.Equipped:Connect(function()
	local char = script.Parent.Parent.Parent
	local hum = char.Humanoid

	local Slash1 = char.Humanoid:LoadAnimation(script.Parent.Slash1)
	local Slash2 = char.Humanoid:LoadAnimation(script.Parent.Slash2)
	idleanim = char.Animate.idle:WaitForChild('Animation1')
	toolnoneanim = char.Animate.toolnone:WaitForChild('ToolNoneAnim')
	local Stance = 'rbxassetid://2602051305'
	local Nothing = 'rbxassetid://2612295642'
	local DamagePoints = script.Parent.Parent.Damage.Value

	isHit = false
	damaging = false
	slashnum = 0
	
	idleanim.AnimationId = Stance
	toolnoneanim.AnimationId = Nothing

	function DoDamage() -- the function
		if damaging == true then
			script.Parent.Parent.MeshPart.Touched:Connect(function(hit)
				if not isHit then
					isHit = true
					print('Hit!')
				
					local hithum = hit.Parent:FindFirstChild('Humanoid')
					if hithum then
						print('Found Humanoid')
						
						hithum:TakeDamage(DamagePoints)
						print(DamagePoints..' Damage dealt')
					end
					wait(0.5)
					isHit = false
					print(hithum.Health..'/'..hithum.MaxHealth..' '..hithum.Parent.Name..'!')
				end
			end)
		end
	end
	
	script.Parent.Parent.Activated:Connect(function()
		if not damaging then
			if slashnum == 0 then
				damaging = true
				
				Slash1:Play()
				print('1')
				DoDamage() -- starting the function
				wait(0.4)
				slashnum = 1
				
				damaging = false
			end
		end
	end)
	script.Parent.Parent.Activated:Connect(function()
		if not damaging then
			if slashnum == 1 then
				damaging = true
				
				Slash2:Play()
				DoDamage() -- starting the function
				wait(0.4)
				slashnum = 0
				
				damaging = false
			end
		end
	end)
end)

2 comments

0

delete the wait after print(DamagePoints..' Damage dealt') end

0

that makes the dummy die faster when the sword is just touching it :/

2 answers

✓ Accepted answer

You never disconnect the function from the .touched event, so it continues to call the function every time it is touched.

2 comments

0

how would i disconnect the function from the .Touched ?

0

Figured it out thanks

no you need to change the

wait(0.5)
isHit = false

to true because as long as its false the function will run or put

is hit = false after if hithum then

2 comments

0

the isHit is set to true a few line before that that's the debounce

0

ye nvm

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