Problem with velocity in script!

I have recently added a throwing mechanic to my game and basically whenever i go to throw it, most of the times it drops down while other times it actually gets thrown:

https://gyazo.com/618a55a0feb377fa6060de795574c770

I don’t know if this is server lag or an error with my script:

local function Throw()
	grabbeditem.Velocity = camera.CFrame.LookVector * 150
end

The throw

mouse.Button2Up:Connect(function()
	facing = false
	grabbing = false
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
		game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
		grabbeditem.Parent.Outline.Transparency = 1
		grabbeditem.BodyPosition:Destroy()
	end
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
	
	Throw()
	
end)

What initiates the throw

local plr = game.Players.LocalPlayer
local char = plr.Character
local grabbing = false
local grabbeditem = nil
local facing = false
local dist = 11
local userinputservice = game:GetService('UserInputService')
local mouse = plr:GetMouse()
local camera = workspace.CurrentCamera
local highlightobject = nil
camera.FieldOfView = 90

repeat wait() until plr.Character

local function Throw()
	grabbeditem.Velocity = camera.CFrame.LookVector * 150
end

mouse.Button1Down:Connect(function()
	if mouse.Target and mouse.Target.Parent:IsA("Model") and mouse.Target.Parent:FindFirstChild("Grabbable") then
		if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
			grabbing = true
			grabbeditem = mouse.Target.Parent.PrimaryPart
			game.ReplicatedStorage.ItemGrabbed:FireServer(grabbeditem)
			local bp = Instance.new("BodyPosition", grabbeditem)
			bp.D = 150
			bp.P = 2000
			grabbeditem.Parent.Outline.Transparency = 0
		end
	end
end)

mouse.Button2Down:Connect(function()
	if grabbing == true then
		facing = true
	end
end)

mouse.Button2Up:Connect(function()
	facing = false
	grabbing = false
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
		game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
		grabbeditem.Parent.Outline.Transparency = 1
		grabbeditem.BodyPosition:Destroy()
	end
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
	
	Throw()
	
end)

mouse.Button1Up:Connect(function()
	grabbing = false
	facing = false
	if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
		game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
		grabbeditem.Parent.Outline.Transparency = 1
		grabbeditem.BodyPosition:Destroy()
	end
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if grabbing == true and grabbeditem ~= nil then
		if (plr.Character.PrimaryPart.Position - grabbeditem.Position).Magnitude < dist then
			grabbeditem.BodyPosition.Position = ((camera.CFrame * CFrame.new(0,0,-(dist - 4))) ).Position
			if facing == true then
				grabbeditem.BodyPosition.Position = ((camera.CFrame * CFrame.new(2,1,-(dist - 8))) ).Position
			end
		else
			grabbing = false
			if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
				grabbeditem.BodyPosition:Destroy()
				game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
			elseif grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
				grabbeditem.BodyGyro:Destroy()
			end
		end
			else
				if mouse.Target ~= nil then
					if mouse.Target.Parent:IsA("Model") then
					if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and mouse.Target.Parent:FindFirstChild("Grabbable")  then
					if (plr.Character.PrimaryPart.Position - mouse.Target.Parent.PrimaryPart.Position).Magnitude < dist * 1.2 and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
					if highlightobject and mouse.Target ~= highlightobject then
						highlightobject.Transparency = 1
					end
					highlightobject = mouse.Target.Parent.Outline
					highlightobject.Transparency = 0
					end
				else
					if highlightobject then
						highlightobject.Transparency = 1
					end
			end
		
				else
				if highlightobject ~= nil then
				highlightobject.Transparency = 1
			end
		end
	end	
	end
end)

the full script

Maybe use debounce? I honestly don’t know but if it doesn’t work I will try to figure out what the problem is.

1 Like

Where do you think i should use the debounce? i don’t really see the use for it

Well, I agree, too. Maybe use a repeat wait() or either increase the distance.

1 Like

increasing the distance returns the same results, where would i use repeat wait() then?

Usually in some cases, it will repeat. It may not work, but if it does I am glad. Here is an example: If I wanted to view somewhere like a part in this case, use it.

1 Like

Re-read my text again. I fixed something.

This looks like an issue to do with network ownership. It seems like the client is attempting to set physics and when the object goes a distance away from the player, the owner changes to the server who has not set any velocity to the object. As such, the server nullifies the velocity that the client applies.

Consider setting the velocity from the server instead of the client when the RemoteEvent is fired. Set the network owner of the item to the server first so that the client cannot wrestle ownership from the server and automatic assignment can be disabled, then apply velocity.

Worked, you’re a genius. Thank you so much!