Why won't my script detect .Touched?

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a sword damage a player.

  2. What is the issue? Include screenshots / videos if possible!
    The .Touched function only will run twice then it won’t do anything anymore. The CanTouch property is set to True and it doesn’t get changed.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I am using print statements, that is how I know that it only runs twice.

Code

--////////////////Config///////////////--
local Damage = 20 --Change this amout to the ammount of dammage you want your weapon to deal 100 is insta kill 
--/////////////////////////////////////--


local SwingEvent = game.ReplicatedStorage.SwingEvent
local Character = script.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Blade = script.Parent.Sword
local CanHit = false

SwingEvent.OnServerEvent:Connect(function(player, Action)
	if Action == "Start" then
		CanHit = true
	else
		CanHit = false
	end
end)

Blade.Touched:Connect(function(Part)
	print("Touched")
	if Part.Name ~= "Scutum" and Part.Parent:FindFirstChild("Humanoid") and CanHit then
		CanHit = false
		print("Is a Body Part")
		script.Parent.Humanoid:TakeDamage(Damage)
		script.Parent.BodyHit:Play()
	elseif Part.Name == "Scutum" and CanHit then
		CanHit = false
		print("Is a Shield")
		script.Parent.WoodShieldHit:Play()
	end
end)

Why are you using script.Parent.Humanoid when you have already declared it as a veriable?

Also, in your variable its script.Parent.Parent.Humanoid but the part where you made it took damage is only script.Parent.Humanoid

Woops, lol, thanks, right there it should have been ‘Part.Parent,Humanoid:TakeDamage(Damage)’. ANd I figured it out now, I accidentally put the script in the wrong place lol. Thanks anyways!

Never mind, now it works three times, but then stops working. Here is my new code;

--////////////////Config///////////////--
local Damage = 20 --Change this amout to the ammount of dammage you want your weapon to deal 100 is insta kill 
--/////////////////////////////////////--


local SwingEvent = game.ReplicatedStorage.SwingEvent
local Character = script.Parent.Parent
local Blade = script.Parent.Sword
local CanHit = false

SwingEvent.OnServerEvent:Connect(function(player, Action)
	if Action == "Start" then
		CanHit = true
	else
		CanHit = false
	end
end)

Blade.Touched:Connect(function(Part)
	print("Touched")
	if Part.Name ~= "Scutum" and Part.Parent:FindFirstChild("Humanoid") and CanHit then
		CanHit = false
		print("Is a Body Part")
		Part.Parent.Humanoid:TakeDamage(Damage)
		script.Parent.BodyHit:Play()
	elseif Part.Name == "Scutum" and CanHit then
		CanHit = false
		print("Is a Shield")
		script.Parent.WoodShieldHit:Play()
	end
end)

Never mind again lol, I forgot to weld the hit detector part to the rest of the sword lol.

1 Like