What is wrong with this velocity code?

I want to achieve when I press the “P” button, it will make the part go forward using AssemblyLinearVelocity. My code I have is:

local p = game.Players
local lp = p.LocalPlayer
local UIS = game:GetService("UserInputService")
local ws = game.Workspace

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.P then
		ws.Part.AssemblyLinearVelocity = Vector3.new(20,0,0)
	end
end)

I do not get any errors from it, but the part moves when the game runs. What could be happening? Thanks in advance!

Edit: I forgot to mention that this is in a LocalScript parented to StarterCharacterScripts

1 Like

Does the part have its linear velocity set to anything in the property window? If so , set it to 0,0,0.

1 Like

Although it did have a value set in it and I set it to 0 once again, it now does not move at all. I have to assume that the problem is in my code.

2 Likes

Yes you atleast know that the code is the issue, we can go from there.

1 Like

Ok read this post’s solution:

1 Like

After modifying my code to match that post’s solution, it still does not move. My newer code now is:

local p = game.Players
local lp = p.LocalPlayer
local UIS = game:GetService("UserInputService")
local ws = game.Workspace

game.Players.ChildAdded:Connect(function(plrAdded)
	game.Workspace.Part:SetNetworkOwner(plrAdded)
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.P then
			ws.Part.AssemblyLinearVelocity = Vector3.new(20,0,0)
		end
	end)
end)

No those modifications wont work… and i believe you will need remote events to move things in server from a local script.

1 Like

I modified it once again to use remote events, yet it still does not work.

Localscript:

local replstore = game.ReplicatedStorage
local addVel = replstore.AddVel
local p = game.Players
local lp = p.LocalPlayer
local UIS = game:GetService("UserInputService")
local ws = game.Workspace
local part = game.Workspace.Part

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.P then
		addVel:FireServer(part)
	end
end)

ServerScript (Located in ServerScriptService):

local replstore = game.ReplicatedStorage
local addVel = replstore.AddVel

addVel.OnServerEvent:Connect(function(player, part)
	game.Workspace.Part.AssemblyLinearVelocity = Vector3.new(30,0,0)
end)

You will also need network ownership in order to apply physics changes.

Ex:

I added a network ownership statement into it, but it is still not working.

local replstore = game.ReplicatedStorage
local addVel = replstore.AddVel

game.Players.PlayerAdded:Connect(function(plr)
		addVel.OnServerEvent:Connect(function(player, part)
			game.Workspace.Part:SetNetworkOwner(plr)
			game.Workspace.Part.AssemblyLinearVelocity = Vector3.new(30,0,0)
	end)
end)

1 Like

Try this:

local replstore = game.ReplicatedStorage
local addVel = replstore.AddVel


addVel.OnServerEvent:Connect(function(player, part)
	game.Workspace.Part:SetNetworkOwner(player)
	game.Workspace.Part.AssemblyLinearVelocity = Vector3.new(30,0,0)
end)

1 Like

I just have one more question about this solution…

After I have pressed P and it goes foward, I cannot use it again.

I believe you will do this:

local connection
connection = UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.P then
		addVel:FireServer(part)
	end
connection:Disconnect()
end)

This seems to make the whole script not work.

LocalScript:

local replstore = game.ReplicatedStorage
local addVel = replstore.AddVel
local p = game.Players
local lp = p.LocalPlayer
local UIS = game:GetService("UserInputService")
local ws = game.Workspace
local part = game.Workspace.MovingObject

local connection
	connection = UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.P then
		addVel:FireServer(part)
	end
	connection:Disconnect()
end)

ServerScript:

local replstore = game.ReplicatedStorage
local addVel = replstore.AddVel

addVel.OnServerEvent:Connect(function(player, part)
	game.Workspace.MovingObject:SetNetworkOwner(player)
	game.Workspace.MovingObject.AssemblyLinearVelocity = Vector3.new(100,0,0)
end)

Do you mean once the part moves it does not stop and so you cannot use the P again since the part is moving now?

No, I mean that the part goes, but then when I press P once again it does not go and does not give me an error.

You set the velocity and so nothing will stop its velocity unless you script it in to do that… its not like a throttle input or a tool that disables once you activate it.

I think you misunderstood what I meant. I meant that when I press P, the part goes, slows down and stops. I then press P once again and it does not move.