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.