Help with .Touched event

I’m using the .Touched event to stop an arrow as soon as it touches another part. This is the event:

script.Parent.Parent.Touched:Connect(function(part)
	if part:IsDescendantOf(workspace.superman990088) then
                print("Don't Stop")
	else
		script.Parent.Parent.Anchored = true
		script.Parent.Velocity = Vector3.new(0, 0, 0)
	end
end)

It works perfectly on the server side as you can see here:

But on the client side the velocity and Anchored get changed before it’s touched the part, so it stops early:

Anyone think they know what’s causing this?

Edit: I’m noticing that the movement of the arrow is very laggy on the client side, but completely smooth on the server side

1 Like

Seems to be an issue with the client being delayed, or something along those lines. A longer fix would be to retrieve the current CFrame of that arrow and fire it to the client to ensure that the position is replicated client-side; although that method isn’t very efficient, it’s the only solution I can offer so far.

It is most likely due to client latency. What have you set the network ownership to? For these types of things, I keep them on the server.

I’m not sure what you mean by network ownership.

I’ll leave the explaining to the API as it explains it quite well. Network ownership comes in handy for projectiles.

2 Likes

You could probably get the cframe/rotation and pass that to the client when it’s hit, and adjust the arrow that way.

I tried the

:SetNetworkOwner(nil)

bit, but it’s coming up with the error that it can’t be called on anchored parts, but this part isn’t anchored or welded to an anchored part

I don’t suggest using .Touched for this type of behavior. Take a look at the FastCast module.

The way you scripted is somewhat bad practice, you can improve this by instead of checking just your player, by checking if the part parent is a player.

script.Parent.Parent.Touched:Connect(function(part)
   if game:GetService("Players"):GetPlayerFromCharacter(part) then
       -- don't do stuff
   else
       --do stuff
   end
end)

Anyways back to the original problem, I HIGHLY suggest looking into raycasting for the arrow. Maybe take a look into this

I created my bow using raycasting, as it is more effective.

Here is an example from my bow, what I did was create a ray in front of the bow, which then if it found something it would stop the arrow.

local newpoint = root.CFrame*Vector3.new(0,5,0)--*speed
	local nextpoint = root.Position+root.CFrame.lookVector
	
	local ray = Ray.new(root.Position,(newpoint-root.CFrame.p))
	print((newpoint-root.CFrame.p))
	local hit,point,normal,material = workspace:FindPartOnRayWithIgnoreList(ray,ignore,false,true)
	if hit then
		--stop
	end

I’d like to use velocity here though since the arrow starts to drop after travelling a certain distance, so it’s more realistic.

Well this is still using it’s own custom arrow physics, the purpose of the ray is to detect if the arrow is going going to collide with something in front of it. And if there is something in front of it, it stops the arrow. The raycasting gun is just an example of how to use raycasting.

blue is the ray
image

So I should keep the arrow velocity changed but replace .touched with a raycast method? Would that stop this?

I removed the .touched part but The client is still very laggy compared to the server

Solved the problem by making the arrow move on all clients rather than the server. Wasn’t what I hoped to do but it works now.