.Touched all of a sudden seems to have stopped working

I have a sword that damages npc.

It was working when I first opened studio; I haven’t edited anything in the local script where the how weapon code is. I’ve simply been working on UI in inkscape and putting them in ImageLabels in my game.

Is this a known issue or regular issue?

Here’s my code just incase it’s something on my part and I’ve simply been getting lucky with it working.

if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		local clickDifference =  tick() - lastclicked
		if clickDifference > 1 then
			lastclicked = tick()
			character:WaitForChild("Greatsword").Touched:Connect(Check)
			if clickDifference < 1.5 then
				combo = (combo + 1) % 2 else
				combo = 0
			end
			if combo == 0 then
				Hit1Track:Play() 
			elseif combo == 1 then
				Hit2Track:Play()
			end
		end
	end

I’ve tested with prints before and after .Touched and they’ve both printed and the animations play correctly.

function Check(hit)
Swinging = true
if Swinging then
	Swinging = false
	if hit.Parent:FindFirstChild("Humanoid") then
		local npc
		for i, v in pairs(game.Players:GetChildren()) do
			if hit.Parent.Name ~= v.Name then
				npc = true elseif hit.Parent.Name == v.Name then
				npc = false
			end
		end
		if npc then
			dealDamage:FireServer(hit.Parent.Humanoid)
		end	
	end
end
end

None of this code runs at all.

You shouldn’t be doing .Touched from a localscript, you should be handling attacks from the server and user input from the client. An exploiter could easily fire dealDamage to damage anyone from the client.

1 Like

Good point.

So I should have edit my first chunk of code to fire to the server then in the server have the .Touched function?

Will that fix the problem though? Is .Touched very buggy client side?

You could use a remote event perhaps.

I don’t work with tools much so it might be different, but I feel like it’s a general sort of rule of thumb that it can be buggy in most use cases.

I would start by first rewriting it and handling the .Touched portion on the server, see if that fixes it, and then if the issue persists afterwards reply to this thread about it.

Okay. I’ve redone the code but the Touched event still won’t run.

This is in the local script.

if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
	local clickDifference = tick() - lastclicked
	if clickDifference > 1 then
		lastclicked = tick()
		if clickDifference < 1.5 then
			combo = (combo + 1) % 2 else
			combo = 0
		end
		if combo == 0 then
			Weapon:FireServer(character.Greatsword, Hit1Track)
		elseif combo == 1 then
			Weapon:FireServer(character.Greatsword, Hit2Track)
		end
	end
end

These two are in the server script.

Weapon.OnServerEvent:Connect(function(player, weapon, animation)
    animation:Play()
    weapon.Touched:Connect(Check)

function Check(hit)
    Swinging = true
    if Swinging then
	    Swinging = false
	    if hit.Parent:FindFirstChild("Humanoid") then
		   local npc
		   for i, v in pairs(game.Players:GetChildren()) do
			    if hit.Parent.Name ~= v.Name then
				    npc = true elseif hit.Parent.Name == v.Name then
				    npc = false
			    end
		    end
		    if npc then
			    hit.Parent.Humanoid:TakeDamage(10)
		    end
	    end
    end
end

My weapon is set to CanCollide = false but that shouldn’t affect it. CollisionFidelity is Default too.

Yeah, you should fire the server when you attack, then check everything from the client. Don’t fire an event to damage something directly.

This may or may not solve your issues, but it’s a lot better to check from the server rather than the client telling the server to damage something. A script inserter could easily do dealDamage:FireServer(game.Workspace.RandomPlayer.Humanoid)

edit: now that I think about it, it might fix it since I’m not sure if you can send instances through an event

I’ll try doing

player.Character[weapon.Name].Touched:Connect(Check)

Hopefully that works.

And you should have the Check function BEFORE you connect it

It seems like you connect .Touched to a function that doesn’t exist yet

I’ve moved the Check() function above now and did

player.Character[weapon.Name].Touched:Connect(Check)

But it still doesn’t work. Really don’t know why.

Might not fix it, but instead of looping through players, you can just do game.Players:GetPlayerFromCharacter(hit.Parent) to check if it’s a player

I changed the CollisionFidelity to Box and it started working again.