Velocities, detecting them, and checking how large they are

What I want is to check velocities of parts flying through the air
This way I can
-Make them do nothing to the player if it’s too slow
-Make them stick into the player if they’re going fast enough
-Make them instantly kill the player if they’re going really fast

How would I go about doing this?

I have my current code written here as a base but it doesn’t really work so

local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

character:WaitForChild("Humanoid").Touched:Connect(function(touchedPart, bodyPart)
	local vel = touchedPart.AssemblyLinearVelocity
	if vel.X > 50 or vel.Y > 50 or vel.Z > 50 or vel.X < -50 or vel.Y < -50 or vel.Z < -50 then
		game.ReplicatedStorage.remote:FireServer(touchedPart)
	end
end)

in startercharacterscripts

Are you using any BodyMovers for the part to move? Also put wait() inside the loop so it wont get laggy.

im an idiot i posted the wrong code

just changed it have another look

and no im not using body movers, im just testing it in studio with the move tool and i can see the assemblylinearvelocity changing so

Is the AssemblyLinearVelocity changing when printing?

yeah, and even when it goes above 50 i dont die

Vectors have a property called Magnitude which represents the length of the vector. It’s basically a scalar. You can use it to detect the speed of an object.

If Velocity.Magnitude > 50 then
1 Like

ill try this, thanks… ill be right back…

heres the new code, anything seem wrong with it before i test it?

local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

character:WaitForChild("Humanoid").Touched:Connect(function(touchedPart, bodyPart)
	local vel = touchedPart.AssemblyLinearVelocity
	if math.abs(vel.Magnitude) > 5 then
		game.ReplicatedStorage.remote:FireServer(touchedPart)
	end
end)

(the remote event is just saying to kill the player and delete the part on the server, i dont think i need to do the player kill part on the server but whatever)

Seems like it could work, also make sure the server side works as well!

which part in my case would be the server side?

Also, if the part stops when it touches the character, there’s a chance that the .Touched() detects 0 magnitude.

  1. i thought there was a chance that happened
  2. if thats the case, how do i account for this?

Actually you don’t need math.abs Since Magnitude represents the length of the Vector it cannot be negative.

Magnitude is basically distance or length. Thats how I like to think of it.

game.ReplicatedStorage.remote:FireServer(touchedPart)

oh… i see, thanks a ton ill change that now

all that does is destroy the part and kill the player

if i kill the player in a local script, does that kill the player for everyone or just the player
like do i have to kill them on the server

It should kill the player and since the player character replicates to the server, everyone should see the player dies.

So killing the player in LocalScript should work.

alright got it

also my previous question, if the problem is the touched detecting the 0 when it stops when it hits the player, how do i fix that?

Probably setting CanCollide to false.

One last thing to note Im not sure Humanoid has a Touched event. You may want to use the HumanoidRootPart