Anchor part on touch

I’m trying to make a part that uses Velocity and I made it so that it jumps off using your mouse’s position.
After when the part jumps off, I use .Touched to check if the part touches something, and if it does, anchor the part. However, the touched event fires even before the part touches something. (anchors before touch)
How do I fix this?

Script:

	local grenade = tool.Handle:Clone()
	grenade.Parent = workspace
	grenade.Transparency = 0
	grenade.CanCollide = false
	grenade.CFrame = tool.Handle.CFrame
	grenade.Velocity = (CFrame.new(grenade.Position, Vector3.new(mousePosition.X, grenade.Position.Y, mousePosition.Z)) * CFrame.Angles(angle, 0, 0)).lookVector * 85
local pov
			pov = grenade.Touched:Connect(function(hit)
				if hit.Parent ~= tool.Parent and hit.CanCollide then
					if hit.Parent.Name ~= player.Name then
						pov:Disconnect()
						if hit.Parent:FindFirstChild("Humanoid") then
							grenade.WeldConstraint.Part0 = grenade
							grenade.WeldConstraint.Part1 = hit
						else
							task.wait(0.7)
							grenade.Anchored = true
						end
						task.wait(5)
						Explode(grenade)
					end
				end
			end)
2 Likes

Since it anchors before touch print out the name of the object hit so we’ll know what the problem is.

I’ve done that, it prints out the part that touched it but in reality, it’s floating.
There are no invisible walls.

(It touched a baseplate, but then anchors before it even touches the baseplate.)

1 Like

Well you did do task.wait(0.7) before anchoring so… Could you provide a clip of the grenade launching including the trajectory and anchoring in the air?

Remove the entire .Touched segment, see if it still anchors or not.

(Without task.wait(0.7))

It does nothing now. (hit) is supposed to be there.

What’s the name of the touched part?

It prints out “Baseplate”.
The floor.

Okay so I replicated your script to the extent that it could be debugged and I noticed that even without equipping the tool the hit on the baseplate is detected.

The reason why is because there are 2 scripts. One for running inside the client, and one for running inside the server. Here’s the file:
grenade.rbxm (36.1 KB)

Yeah I figured that a localscript is required to get the player’s mouse but I cba to write that so I’m just gonna debug from here using set variables

From what I can see, this behavior is caused by server replication issues. If you remove the grenade:SetNetworkOwner(nil) and let the player calculate the physics, it actually works just fine. In order to set the network owner to the server and also make it not fire too early, we can use a different approach.

Try replacing .Touched with RunService.Heartbeat.

local pov
pov = game:GetService("RunService").Heartbeat:Connect(function()
	local detect = workspace:GetPartsInPart(grenade)

	for _,hit in ipairs(detect) do
		local model = hit:FindFirstAncestorWhichIsA("Model")
		local hum = model and model:FindFirstChildWhichIsA("Humanoid")

		if model ~= tool.Parent then
			pov:Disconnect()
			if hum then
				grenade.WeldConstraint.Part0 = grenade
				grenade.WeldConstraint.Part1 = hit
			else
				grenade.Anchored = true
			end
			task.wait(5)
			Explode(grenade)
		end
	end
end)
1 Like

Beat me to it.
I got it working with mainly the original code just now so here you go anyways

local tool = script.Parent

tool.Activated:Connect(function()
	local plr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
	print("activated")
	local grenade = tool.Handle:Clone()
	grenade.Parent = game.Workspace
	print(grenade.Position)
	grenade.Transparency = 0
	grenade.CanCollide = false
	grenade.CFrame = tool.Handle.CFrame
	
	grenade.Velocity = (CFrame.new(grenade.Position, Vector3.new(-16, grenade.Position.Y, -8.5)) * CFrame.Angles(45, 0, 0)).lookVector * 85
	
	local pov = grenade.Touched:Connect(function(hit)
		print(hit.Name)
		if hit.Parent ~= tool.Parent and hit.CanCollide then
			print(hit.Parent.Name)
			print(plr)
			if hit.Parent.Name ~= plr.Name then
				--pov:Disconnect()
				if hit.Parent:FindFirstChild("Humanoid") then
					grenade.WeldConstraint.Part0 = grenade
					grenade.WeldConstraint.Part1 = hit
				else
					grenade.Anchored = true
				end
				task.wait(5)
				print("explosion")
			end
		end
	end)
end)