I want to make a game where one of the mechanics is you press a or d for to slide left or right. Currently I only have it so a variable "lanes" increases of decreases when you press "d" or "a", it doesn't move the character. my first idea is to move the player using body forces, if there is anything else i can do to do something similar tell me. later, i want to animate the slides, but i want to finish game mechanics.
local lanes = 0 --slide right function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.D then print("D was pressed") lanes = lanes + 1 print(lanes) print("--------------") end end --calls slide right game:GetService("UserInputService").InputBegan:connect(onKeyPress) --slide left function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.A then print("A was pressed") lanes = lanes - 1 print(lanes) print("--------------") end end --calls slide left game:GetService("UserInputService").InputBegan:connect(onKeyPress) --overflow control keeps lanes between -2 and 2. (doesn't work yet) while lanes == 3 do lanes = lanes - 1 end while lanes == -3 do lanes = lanes + 1 end
Lets make this work with filtering enabled, We will use your script you made to decide the lanes value, then we will send those over to the server through a remoteevent, multiplying it by the variable Mul(default is 200) then set the body force's X to that, it will create a body force when the local script loads, resetting will not cause a problem for it will redo it.
place a RemoteEvent inside workspace called "Lane"
LocalScript inside StarterPack
workspace:WaitForChild(game.Players.LocalPlayer.Name) workspace:WaitForChild("Lane"):FireServer(0,true) local lanes = 0 --slide right function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.D then print("D was pressed") lanes = lanes + 1 print(lanes) print("--------------") workspace.Lane:FireServer(lanes) end end --calls slide right game:GetService("UserInputService").InputBegan:connect(onKeyPress) --slide left function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.A then print("A was pressed") lanes = lanes - 1 print(lanes) print("--------------") workspace.Lane:FireServer(lanes) end end --calls slide left game:GetService("UserInputService").InputBegan:connect(onKeyPress) --overflow control keeps lanes between -2 and 2. (doesn't work yet) while lanes == 3 do lanes = lanes - 1 end while lanes == -3 do lanes = lanes + 1 end
Regular script inside workspace
local rem=workspace:WaitForChild("Lane") local mul = 200 rem.OnServerEvent:connect(function(player,lane,it) if it~=nil and it==true then if workspace:FindFirstChild(player.Name)~=nil then if workspace:FindFirstChild(player.Name):FindFirstChild("UpperTorso")~=nil then Instance.new("BodyForce",workspace:FindFirstChild(player.Name).UpperTorso).Force=Vector3.new(0,0,0) elseif workspace:FindFirstChild(player.Name):FindFirstChild("Torso")~=nil then Instance.new("BodyForce",workspace:FindFirstChild(player.Name).Torso).Force=Vector3.new(0,0,0) end end elseif it==nil then if workspace:FindFirstChild(player.Name)~=nil then if workspace:FindFirstChild(player.Name):FindFirstChild("UpperTorso")~=nil then if workspace:FindFirstChild(player.Name).UpperTorso:FindFirstChild("BodyForce")~=nil then local t = workspace:FindFirstChild(player.Name).UpperTorso workspace:FindFirstChild(player.Name).UpperTorso.BodyForce.Force=Vector3.new(((t.CFrame.rightVector.X)*lane)*mul,0,((t.CFrame.rightVector.Z)*lane)*mul) end elseif workspace:FindFirstChild(player.Name):FindFirstChild("Torso")~=nil then if workspace:FindFirstChild(player.Name).Torso:FindFirstChild("BodyForce")~=nil then local t = workspace:FindFirstChild(player.Name).Torso workspace:FindFirstChild(player.Name).Torso.BodyForce.Force=Vector3.new(((t.CFrame.rightVector.X)*lane)*mul,0,((t.CFrame.rightVector.Z)*lane)*mul) end end end end end)
Hoped this helped if you have any questions please do ask :)