The touched event fire before hitting anything

What do you want to achieve? I want the kunai to stop moving once it’s hit something

What is the issue? the kunai stop before hitting anything
here’s the video

here’s the script

rep_storage = game:GetService("ReplicatedStorage")
through_kunai = rep_storage:WaitForChild("through_kunai")
server_storage = game:GetService("ServerStorage")
kunai = server_storage:WaitForChild("kunai")
through_kunai.OnServerInvoke = function(plr, kunai_position)
	local character = plr.Character
	local humanoid_root_part = character.HumanoidRootPart
	local new_kunai = kunai:Clone()
	new_kunai:SetPrimaryPartCFrame(CFrame.new(humanoid_root_part.Position, kunai_position))
	local body_velocity = Instance.new("BodyVelocity", new_kunai.PrimaryPart)
	body_velocity.MaxForce = Vector3.new(100000,100000,100000)
	body_velocity.Velocity = new_kunai.PrimaryPart.CFrame.LookVector * 70
	new_kunai.Parent = workspace
	new_kunai.PrimaryPart.Touched:Connect(function(hit)
		if not hit:IsDescendantOf(character) then
			print(hit)
			body_velocity.Velocity = Vector3.new(0,0,0)
			new_kunai.PrimaryPart.Anchored = true
		end
	end)
end

That’s because the part is touching the baseplate. You should make an if statement checking if the hit part is the baseplate (or the floor).

it didn’t touch the baseplate if you look at the out put it said “part” that means it touched the part that was thrown at

new_kunai.PrimaryPart.Touched:Connect(function(hit)
		if not hit:IsDescendantOf(character) then
			print(hit)
			body_velocity.Velocity = Vector3.new(0,0,0)
			new_kunai.PrimaryPart.Anchored = true
		end
	end)

Oh yeah, I was misunderstanding. I think the PrimaryPart may be invisibly hitting the part, but the hitbox is larger than the visual mesh.

1 Like

nope, the primarypart is the same size as the mesh and i add weld constrain too

Hmm, very strange.

Just for experimental purposes, try slowing down the BodyVelocity, to see if maybe it’s just going too fast to register. Let me know the results.

that actually work, when i reduce the speed to 20 or 50 it works fine but if i go above that the same problem will happen again. but i need it to go a least 100 speed or it’s too slow

Mkay, so we identified our issue, now we are looking for the solution.

My only thoughts are making a delay between the Touched event firing and setting the Velocity to 0, using a wait. But, this seems like it’s unreliable and inaccurate.

I’m going to do some research on it, and let you know what I find.

Another possible solution is making the hitbox a bit smaller, so that it’s from the end of the handle to the start of the blade. But don’t make it go through the blade. Let me know how that works.

nope, it still the same as before
dasdas

This solution is a little clunky, but what you could try is making it so that if hit.Name == part then return. The only problem is you’d have to make sure nothing it could collide with is named part. I don’t recommend this as a long-term solution but it might work for now.
Or maybe try turning canTouch off for every part of the knife except the main hitbox.

nope, did not work. :confused: is there any method that is similar to Touched

You could use the GetTouchingParts function, but that would require using a loop.

I don’t see anything wrong however try doing this to avoid memory leak :

local Conn
Conn=new_kunai.PrimaryPart.Touched:Connect(function(hit)
		if not hit:IsDescendantOf(character) then
			print(hit)
			body_velocity.Velocity = Vector3.new(0,0,0)
			new_kunai.PrimaryPart.Anchored = true
            Conn:Disconnect()
		end
	end)

Also you could try changing the Hit detection to raycasting or GetTouchingParts.

maybe it’s lag or something because
server pov:


client pov:

both kunai position are difference

Oh well thats a replication issue , I would suggest to set the network owner of the Kunai as the Client , handle the physics in client and detect the hit in client . Once it hits a character fire a remote event to the server and do some sanity checks to prevent exploiting, and in the client stop the movement.

Put this on the next line. This could potentially fix the replication issue.

wait()
new_kunai:SetPrimaryPartCFrame(new_kunai:GetPrimaryPartCFrame())

from my experience of making an arrow i found that my arrow was hitting the player which resulted in it being in air so i guess it might be the same problem with you where its hitting the character and stops

well nope, it didnt work :confused: