Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to keep a player from moving on a certain axis?

Asked by
LeeRob 2
5 years ago

My first ROBLOX game is going to be 2D, so I only want the player to use A and D to move and use W to jump and S to crouch, so W and S still have to be useable. I'm not sure if it's necessary, but here's my local script for jumping:

--LOCAL SCRIPT
--PUT THIS SCRIPT INSIDE OF STARTERGUI

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()

local jumpEvent = game:GetService("ReplicatedStorage"):WaitForChild("jumpEvent") --Waits for the remoteEvent to be created by the server

mouse.KeyDown:Connect(function(key)
    if string.lower(key) == "w" then
        jumpEvent:FireServer() --send a message to the server to tell the player to jump
    end
end)

and here's my server script:

--SERVER SCRIPT
local jumpEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")) -- create the remoteEvent
jumpEvent.Name = "jumpEvent"
jumpEvent.OnServerEvent:Connect(function(player) -- receive the message from the local script
    print("Jumped")
    player.Character.Humanoid.Jump = true
end)

I've tried to change the property in StarterPlayer to scriptable and add each control individually, but I can't figure out what event to use that makes you walk in a certain direction because most of them are used to teleport the player. Also, I'm not sure how to make the player continue walking when holding the button down.

Here's what I tried in my local script for moving right:

--LOCAL SCRIPT
--PUT THIS SCRIPT INSIDE OF STARTERGUI

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()

local moveRight = game:GetService("ReplicatedStorage"):WaitForChild("moveRight") --Waits for the remoteEvent to be created by the server

mouse.KeyDown:Connect(function(key)
    if string.lower(key) == "d" then
        moveRight:FireServer() --send a message to the server to tell the player to jump
    end
end)

I've also tried to change the if then part to a while loop but that just loops the action forever.

Here's the server script:

--SERVER SCRIPT
local moveRight = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")) -- create the remoteEvent
moveRight.Name = "moveRight"
moveRight.OnServerEvent:Connect(function(player) -- receive the message from the local script
    print("Moved right.")
    player.Character.Humanoid:Move(Vector3.new(0,16,0), true)
end)

The move right scripts doesn't seem to do anything, but I can't think of anything else that would work.

I hope that you guys can help! Thanks!

Answer this question