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
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)
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)
local connection
connection = UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.P then
addVel:FireServer(part)
end
connection:Disconnect()
end)
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)
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.