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)
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?
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)
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)