Hey there!
I have a script that moves the player up or down depending on if they press e or q, but I can’t figure out how to script it so that the players continue moving up or down until you release e or q. Right now, you have to press e or q over and over to move up/down.
Here’s my script:
local remote = game.ReplicatedStorage.Up
remote.OnServerEvent:Connect(function(plr)
local left = Vector3.new(0,300,0)
plr.Character.HumanoidRootPart.Position = plr.Character.HumanoidRootPart.Position + Vector3.new(0,1,0)
end)
Any and all help is appreciated. Thanks!
Here’s the local script:
local remote = game.ReplicatedStorage:WaitForChild("Up")
local remote1 = game.ReplicatedStorage:WaitForChild("Stop")
local uis = game:GetService("UserInputService")
game:GetService("UserInputService").InputBegan:Connect(function(iobj, gp)
if gp then return end
if iobj.KeyCode == Enum.KeyCode.E then
remote:FireServer()
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(iobcj, gap)
if gap then return end
if iobcj.KeyCode == Enum.KeyCode.Q then
game.ReplicatedStorage.Down:FireServer()
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.E then
print("you have stopped");
-- your code
game.ReplicatedStorage.Stop:FireServer()
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.Q then
print("you have stopped");
-- your code
game.ReplicatedStorage.Stop:FireServer()
end
end)
If you want movement scripts to be smoother have the character move themselves on the client to get instant feedback. Changes will be replicated to the server on their own because by default players get ownership of their own characters (so that it is snappy with minimum input delay).
Just replace your server sided code and add it into your local script. By default Roblox assigns the network ownership so you don’t have to worry about that.
About the issue with needing to press the button over and over, I’m not very confident on an effective method that I know would work without possible conflicts.
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humrootpart = chr:WaitForChild("HumanoidRootPart")
local upVBodyelocity
local downBodyVelocity
local speed = 30
game:GetService("UserInputService").InputBegan:Connect(function(iobj, gp)
if gp then return end
if iobj.KeyCode == Enum.KeyCode.E then
upVBodyelocity = Instance.new("BodyVelocity")
upVBodyelocity.P = math.huge
upVBodyelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
upVBodyelocity.Velocity = Vector3.new(0, 1, 0) * speed
upVBodyelocity.Parent = humrootpart
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(iobcj, gap)
if gap then return end
if iobcj.KeyCode == Enum.KeyCode.Q then
downBodyVelocity = Instance.new("BodyVelocity")
downBodyVelocity.P = math.huge
downBodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
downBodyVelocity.Velocity = Vector3.new(0, -1, 0) * speed
downBodyVelocity.Parent = humrootpart
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.E then
print("you have stopped");
upVBodyelocity:Destroy()
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.Q then
print("you have stopped");
downBodyVelocity:Destroy()
end
end)