Replacing BodyVelocity with an up-to-date object

This may be a trivial question but how can I make this code up-to-date? I’m trying to find an exact replacement of BodyVelocity using the newer LinearVelocity but it’s not working as expected.

script.Parent.MouseButton1Click:Connect(function()
	local character = script.Parent.Parent.Parent.Parent.Character
	
	local bv = Instance.new("BodyVelocity") -- Body movers are deprecated.
	bv.Velocity = Vector3.new(math.random(-10, 10), 0, math.random(-10, 10))
	bv.Parent = character:FindFirstChild("Head")
	game:GetService("Debris"):AddItem(bv, 0.1)
end)

Hi, you should use linear velocity. Because BodyVelocity is deprecated. You should create a linear velocity, and to work it must include attachment, after you should set velocity property (vector3).
There was a topic about it…

1 Like

This doesn’t help me much. I have already tried using LinearVelocity (as I mentioned in my post). However it doesn’t seem to be exactly the same - in many cases the character gets knocked out really far away and also dies. Another problem is that when I jump with LinearVelocity, the body spins in an uncontrollable way (happened when I compared both scripts with the Debris lines removed).

The script which uses LV:

script.Parent.MouseButton1Click:Connect(function()
	local character = script.Parent.Parent.Parent.Parent.Character
	
	local head = character:FindFirstChild("Head")
	if head then
		local attachment = Instance.new("Attachment")
		local lv = Instance.new("LinearVelocity")
		lv.MaxForce = math.huge
		lv.VectorVelocity = Vector3.new(math.random(-3, 3), 0, math.random(-3, 3))
		attachment.Parent = head
		lv.Parent = head
		lv.Attachment0 = attachment
		game:GetService("Debris"):AddItem(lv, 0.1)
		game:GetService("Debris"):AddItem(attachment, 0.1)
	end
end)

With LV:


With BV:

Is there something wrong with the way I am setting the properties/ the way of destroying the objects?

I tested this script that uses LinearVelocity and I’m not getting unstable behavior from it:

local Debris = game:GetService("Debris")

local button = script.Parent

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- Else character will be temporarily nil

player.CharacterAdded:Connect(function(newCharacter) -- So the button works after the character dies
	character = newCharacter
end)

button.MouseButton1Click:Connect(function()
	if character.PrimaryPart then
		local attach = Instance.new("Attachment", character.PrimaryPart)

		local linearVelocity = Instance.new("LinearVelocity")
		linearVelocity.Attachment0 = attach
		linearVelocity.ForceLimitsEnabled = false -- Does the same as setting MaxForce to math.huge
		linearVelocity.VectorVelocity = Vector3.new(math.random(-3, 3), 0, math.random(-3, 3))
		linearVelocity.Parent = character.PrimaryPart

		Debris:AddItem(linearVelocity, 0.1)
		Debris:AddItem(attach, 0.1)
	end
end)

Even when I jump and click the button at the same time, I’m never flung into space although the character does spin in the spot for a very short duration

I found parenting the LinearVelocity to the character’s PrimaryPart/HumanoidRootPart works much better and more stable than using the character’s head

By the way: Since I’m using LocalPlayer the script is a LocalScript, but if you need to use a server script an easy solution is to replace local player = game:GetService("Players").LocalPlayer with: local player = button:FindFirstAncestorWhichIsA("Player")

Okay so replacing the lv.MaxForce = math.huge with lv.ForceLimitsEnabled = false did help with the flickering somehow. Here is my updated code:

script.Parent.MouseButton1Click:Connect(function()
	local character = script.Parent.Parent.Parent.Parent.Character
	
	local head = character:FindFirstChild("Head")
	if head then
		local attachment = Instance.new("Attachment")
		local lv = Instance.new("LinearVelocity")
		lv.ForceLimitsEnabled = false
		--lv.MaxForce = math.huge
		lv.VectorVelocity = Vector3.new(math.random(-3, 3), 0, math.random(-3, 3))
		attachment.Parent = head
		lv.Parent = head
		lv.Attachment0 = attachment
		game:GetService("Debris"):AddItem(lv, 0.1)
		game:GetService("Debris"):AddItem(attachment, 0.1)
	end
end)

I guess my question is answered even though I still don’t fully like the end effect. With BodyVelocity everything seemed more smooth, the effect I was looking for.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.