Touched event firing too early?

Hello there, I am having an issue where my projectiles kinda seem like they are firing their touched event too early. So I have projectiles flying from around 400 studs (Y-Axis Specifically) to 0-10 studs, this is done with the roblox physics engine with the part being un-anchored. I made it so that when the projectile hits the baseplate part called (“SpecificPart”) it prints outs its position. The issue I am having with this is that although the environment ranges from 0-10 studs on the Y Axis, it prints out a Y value, most of the time greater than 20-30 studs, making the touched position inaccurate. I’m not sure if it may be due to server lag or connection lag? Are there any ways to prevent this from happening? The part is around a size of (10,10,10). Please help me to get a more accurate position!

Look at the Y-values being printed out, here is what 3 projectiles print out:
s
Even though my terrain environment ranges from 0-10, it prints out a Y position, sometimes more than 20-30. It also seems that when the part anchors, it anchors in the air.

Here is the code for it:

local enabled = false

function Active(touched)
	if touched.Name == ("SpecificPart") and script.Parent.Position.Y < 150 and enabled == false then
		enabled = true
		wait()
		script.Parent.Anchored = true
		print(script.Parent.Position)
		script.Parent:Remove()
	end
end

script.Parent.Touched:Connect(Active)
2 Likes

When you anchor the part, does it anchor in the same position as the printed position?

1 Like

could you provide maybe a gif for us to visualize what exactly youre facing?

1 Like

@CaptinLetus Yes it does.
@minkmink I will make that right now.

Here is a video showing the parts falling, then getting anchored, the output shows at the end of the video.

Could you attach a Roblox place file for me to look at? It will be much easier to debug if I can reproduce it myself.

This is a problem with roblox replication. Instead of using a .Touched event, I’d do something like:

local connection
connection = game:GetService"RunService".Heartbeat:Connect(function()
    local ray = Ray.new(part.Position, Vector3.new(0, -1, 0) * 3)
    local hitPart, hitPos = workspace:FindPartOnRay(ray)
    if hitPart and hitPart.CanCollide then
        part.CFrame = hitPos
        part.Anchored = true
        connection:Disconnect()
    end
end)
7 Likes

This seemed to solve the replication issue, thanks! Even though the replication is off from the server, the ray cast updates it in this line with: script.Parent.Position = hitPos which updates it with the server.

I’ve fixed up the code a bit:

local connection
connection = game:GetService"RunService".Heartbeat:Connect(function()
    local ray = Ray.new(script.Parent.Position, Vector3.new(0, -1, 0) * 10)
    local hitPart, hitPos = workspace:FindPartOnRay(ray)
    if hitPart and hitPart.CanCollide then
        script.Parent.Position = hitPos
        script.Parent.Anchored = true
        connection:Disconnect()
    end
end)
3 Likes