Touch events for knife not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a simple knife weapon.
  2. What is the issue? Include screenshots / videos if possible!
    The touch events won’t work.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried messing with the touched events but no luck.

Stuff:

--Client script
local tool = script.Parent.Parent
local remotesFolder = tool:WaitForChild("Remotes")
local sliceEvent = remotesFolder:WaitForChild("SliceEvent")
local animsFolder = tool:WaitForChild("Anims")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local walkAnim = humanoid:LoadAnimation(animsFolder:WaitForChild("WalkAnim"))
local slice1Anim = humanoid:LoadAnimation(animsFolder:WaitForChild("Slice1Anim"))
local throttle = false
local function doSliceAttack()
if not throttle then
throttle = true
sliceEvent:FireServer()
slice1Anim:Play()
wait(1.5)
throttle = false
end
end
tool.Activated:Connect(doSliceAttack)

--Server script
local tool = script.Parent.Parent
local remotesFolder = tool:WaitForChild("Remotes")
local sliceEvent = remotesFolder:WaitForChild("SliceEvent")
local murderKnife = tool:WaitForChild("Handle")
local effectsFolder = tool:WaitForChild("Effects")
local knifeTrail = effectsFolder:WaitForChild("KnifeTrail")
local soundsFolder = tool:WaitForChild("Sounds")
local equipSound = soundsFolder:WaitForChild("EquipSound")
local sliceSound = soundsFolder:WaitForChild("SliceSound")

local throttle = false
local hasBeenTouched = {}

local function doSliceAttack()
	if not throttle then
		throttle = true
		knifeTrail.Enabled = true
		local steppedConnection = murderKnife.Touched:Connect(function(touched)
			local touchedParent = touched.Parent
			if not hasBeenTouched[touchedParent] then
				hasBeenTouched[touchedParent] = true
				local humanoid = touchedParent:FindFirstChild("Humanoid")
				if humanoid then
					humanoid:TakeDamage(100)
					sliceSound:Play()
				end
			end
		end)
		wait(0.75)
		knifeTrail.Enabled = false
		wait(0.25)
		steppedConnection:Disconnect()
		hasBeenTouched = {}
		wait(1.5)
		throttle = false
	end
end

sliceEvent.OnServerEvent:Connect(doSliceAttack)

you don’t need remote events for tool.Activated event, you can do it in a server script.

tool.Activated:Connect(function()
        	if not throttle then
        		throttle = true
        		knifeTrail.Enabled = true
        		local steppedConnection = murderKnife.Touched:Connect(function(touched)
        			local findHumanoid = touched.Parent:FindFirstChild("Humanoid")
					if findHumanoid then
						if not table.find(hasBeenTouched, touched.Parent) then
							table.insert(hasBeenTouched, touched.Parent)
							findHumanoid:TakeDamage(100)
						end
					end
				end)
        		wait(0.75)
        		knifeTrail.Enabled = false
        		wait(0.25)
        		steppedConnection:Disconnect()
				for i, v in pairs(hasBeenTouched) do
					--here you can do something with players that have been killed, like sending them to lobby
				end
        		hasBeenTouched = {}
        		wait(1.5)
        		throttle = false
        	end
        end)
1 Like

Are there any errors in the output?

1 Like

No there aren’t any errors in the output.

Yes but does that influence the touch registration at all?

touch may be slightly delayed sometimes

1 Like

Does it work sometimes or does it not work at all?

1 Like

It works sometimes. It works roughly every other time.

It isn’t slightly delayed. It downright doesn’t work.

You have to increase the hitbox size. .Touched is very, very, very unreliable…

1 Like

Is there anything better than touched you would suggest?

Region3, raycasting, GetTouchingParts()

1 Like

best one and tips on how to use it?

Comes down to personal preference…
developer.roblox.com will aid you with all of these.

1 Like