Sword is glitched!

Hey there!

I’m making a sword but there is a glitch. If you equip it, then click a lot, and then you hit someone it instantly kills them instead of doing 10 damage. Basically you can stack the amount of damage by mass clicking it and then once the sword hits the character they automatically die.

Here is the code;

local deb = false

script.Parent.Activated:Connect(function()
	
	if deb == false then
		deb = true
		
		local plr = game:GetService("Players").LocalPlayer
		local Character = plr.Character or plr.CharacterAdded:Wait()

		local AnimationID = "rbxassetid://10481516499" 
		local Animation  = Instance.new("Animation")
		Animation.Parent = Character
		Animation.AnimationId = AnimationID
		Animation.Name = "Slash"

		local LoadedAnimation = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)

		LoadedAnimation:Play()
		
		
		script.Parent.Bottom.Touched:Connect(function(hit)
			if deb == true then
				if hit.Parent.Name == "Dummy" then
					hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
					task.wait()
				end
			else
				if deb == false then
					return
				end
			end
		end)	
		
		task.wait(3)
		
		deb = false
	else
		return
	end
end)

1 Like

Everytime the player is able to activate the tool, you are connection a new Touched event to reduce -10 of Health. Meaning, if player activated the tool 10 times, that would be 10 connections reducing 10 from the target’s health (10x10 = 100, player dies).

Grab that Touched event connection and take it out from the Activated event. Or store the connection into a table and check if connection exist before creating it again.

2 Likes

Would a remote event work? I’m not sure how I would go at it.

@xDeltaXen

why?

Its not about remote events, you should connect the Touched event only once, maybe when the Tool is equipped or when the script runs. You are connecting the Touched event every time a player use the Tool, causing many connections to exist

Get rid of task.wait() and change task.wait(3) to wait(3).

Yes, but then whenever the tool is equipped the animation would play.

I just mean the connection of the Touched event that deals damage, something like this:
I suppose script.Parent is the Tool itself.

When player equips the tool, the Touched event is connected only once, causing 10 damage. When players Unequip the tool, the connection is disconnected.

local deb = false

local ConnectionToDelete

script.Parent.Equipped:Connect(function()
	ConnectionToDelete = script.Parent.Bottom.Touched:Connect(function(hit)
		if deb == true then
			if hit.Parent.Name == "Dummy" then
				hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
				task.wait()
			end
		else
			if deb == false then
				return
			end
		end
	end)	
end)

script.Parent.Unequipped:Connect(function()
	ConnectionToDelete:Disconnect()
end)

script.Parent.Activated:Connect(function()
	if deb == false then
		deb = true
		
		local plr = game:GetService("Players").LocalPlayer
		local Character = plr.Character or plr.CharacterAdded:Wait()

		local AnimationID = "rbxassetid://10481516499" 
		local Animation  = Instance.new("Animation")
		Animation.Parent = Character
		Animation.AnimationId = AnimationID
		Animation.Name = "Slash"

		local LoadedAnimation = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)

		LoadedAnimation:Play()
		
		task.wait(3)

		deb = false
	else
		return
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.