Need help with a script that limits player movement and help with a script that changer the players speed

Here is some gameplay of my game:
Video on Game

I want to make a 2D game. To do that I need a working script that makes it so the player can only move left, right, and jump. I also want a script for a part that changes the players speed or a script that changes the players speed when spawned. (Either will work for what I’m doing) Also this game is not made with ui it is made with parts.

The issue is that:
A. Sometimes the script I used fails and allows the player to move all directions.
B. When I use the speed changing part it allows the player to move all directions when I make it set the player movement speed to 0 then wait 7.5 seconds (Will likely change)
and then set it to 10

I am not really sure on how to fix the problem because I didn’t make the limited player movement script. I looked on here and I didn’t find anything like what I was doing.

These are the scripts I used:

Speed Changer:

local WaitTime = false

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		if WaitTime == false then
			WaitTime = true
			Humanoid.WalkSpeed = 10
		end
	end
end)

Limited Player Movement Script:

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false
local leftValue, rightValue = 0, 0

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		leftValue = 1
	elseif inputState == Enum.UserInputState.End then
		leftValue = 0
	end
end

local function onRight(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		rightValue = 1
	elseif inputState == Enum.UserInputState.End then
		rightValue = 0
	end
end

local function onJump(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		jumping = true
	elseif inputState == Enum.UserInputState.End then
		jumping = false
	end
end

local function onUpdate()
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		if jumping then
			player.Character.Humanoid.Jump = true
		end
		local moveDirection = rightValue - leftValue
		player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
	end
end

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "d", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "a", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction("Jump", onJump, true, " ", Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)

The speed changing part is right above spawn.

Additional Stuff

This is my first time posting here so if there is anything I could change to make better about my future posts then please mention it if you can. I don’t know a lot about how to script but I know a little.

Put this script in local script -

local player = game.Players.LocalPlayer
local character = player.Character
local UIS = game:GetService("UserInputService")

local keybinds = {
Enum.KeyCode.A;
Enum.KeyCode.S;
Enum.KeyCode.D;
};

UIS.InputBegan:Connect(function(input)
if keybinds[input.KeyCode.EnumType] then
character:FindFirstChild("Humanoid").WalkSpeed = 0
end
end)
UIS.InputEnded:Connect(function(input)
if keybinds[input.KeyCode.EnumType] then
character:FindFirstChild("Humanoid").WalkSpeed = 16
end
end)

LocalScript should be inside StarterPlayerScripts.

@Moneypro456789 I did all of that yet for some reason it didn’t work. It worked on the first attempt but after that it stopped working. I disabled the script that changed the speed of the player. I have absolutely no idea why the script worked the first time but not after that. I made an edit on the script and it stopped working. I tried to make it work again by even making a new script and pasting the script you made in again. I put it inside StarterPlayerScripts too. Here is a video of what is happening with the script:
robloxapp-20230104-1614512.wmv (247.6 KB)
It also shows how my limited movement script fails sometimes. Thanks for the reply though :slightly_smiling_face:

1 Like

Can you show me your new script though ?

I also updated the script, try it please and tell me, it should work.

The script still didn’t work. I don’t know why it didn’t. I used a created a new local script and put it under StarterPlayerScripts. My two other local scripts are under StarterPlayer in StarterCharacterScripts (one for camera angle and one for limited movement). Those 2 scripts must be there otherwise they will not work. I wish I knew what to do. There is also the fact that the script that limits movement sometimes fails but I have yet to see it when testing the game outside of studio.
Also you have a typo in your script, the third time you put “local” you spelt it like this:

locaL

instead of

local

which I did change.

Hmmm…I once again updated my script, hopefully it should work.

I doesn’t stop the player from moving still, and this time with your script enabled the script that limits movement to left(D) right(A) and jump(space) fails every time. I had to switch the keys for left and right because of the camera angle btw.

Ok, so what I now suggest is don’t use tables, try making InputBegan function for each A,S and D.

I don’t know what tables are other than the thing you sit around and eat at. If you’re talking about the limited movement script I don’t understand it. I think I got it off of here when I was looking for stuff to make a 2D game.

script.Parent.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if Humanoid then
if WaitTime == false then
WaitTime = true
Humanoid.WalkSpeed = 0
end
end
end)

local Players = game:GetService(“Players”)

local function Touched(part)
if part:IsDescendantOf(Players.LocalPlayer.Character) and part.Name == “HumanoidRootPart” then
Players.LocalPlayer.Character:MoveTo(Vector3.new(3.5, 0, -17.5))
end
end

script.Parent.Touched:Connect(Touched)

I think I found the answer
local Players = game:GetService(“Players”)

local function Touched(part)
if part:IsDescendantOf(Players.LocalPlayer.Character) then
Players.LocalPlayer.Character:MoveTo(Vector3.new(3.5, 0, -17.5))
end
end

script.Parent.Touched:Connect(Touched)

Could you possibly add some instructions like maybe where to put this? You can also make things into code by doing this: three backticks which is this character ` and then hitting enter twice and doing 3 more backticks. it should look something like this.

script.Parent.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if Humanoid then
if WaitTime == false then
WaitTime = true
Humanoid.WalkSpeed = 0
end
end
end)

local Players = game:GetService(“Players”)

local function Touched(part)
if part:IsDescendantOf(Players.LocalPlayer.Character) and part.Name == “HumanoidRootPart” then
Players.LocalPlayer.Character:MoveTo(Vector3.new(3.5, 0, -17.5))
end
end

script.Parent.Touched:Connect(Touched)

--I think I found the answer
local Players = game:GetService(“Players”)

local function Touched(part)
if part:IsDescendantOf(Players.LocalPlayer.Character) then
Players.LocalPlayer.Character:MoveTo(Vector3.new(3.5, 0, -17.5))
end
end

script.Parent.Touched:Connect(Touched)

Your script has a few errors in it though when I went to put it into a script just to check it.