Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

2D Lane switcher. Problems switching to the Left/Right lanes. What have I done wrong?

Hey everyone, was just creating a 2d line runner game and needed help with the player being able to press "left/right" to switch lanes.

This is the turn script.

right = game:GetService("UserInputService")
left = game:GetService("UserInputService")
plr = game.Players.LocalPlayer
Hplr = plr:GetPlayerFromCharacter(plr)
x = Vector3.new(math.random(1,10),0,0)
z = Vector3.new(0,0,math.random(1,10))



local function GoLeft()
	if Hplr.HumanoidRootPart then
		plr.Position += x
	end  
end

local function GoRight()
	plr:Move(Vector3.new(0,0,z))
end

left.InputBegan:Connect(function(left)
	if left.KeyCode == Enum.KeyCode.Left
	then
		GoLeft()
	end
end)

right.InputBegan:Connect(function(right)
	if right.KeyCode == Enum.KeyCode.Left
	then
		GoRight()
	end		
end)

would be happy to know what I've done wrong. I'm pretty new to scripting.

Thanks again c=

Edit: Script is in starterPlayerScripts btw

3 comments

0

you can just use CFrame

0

also which script are you using, it should be an script not a local script, also try putting this script in serverscriptservice

0

Ok so I've gotten a script from @Packayee but now Im recieving the error of "x/z can't be assigned to" any clues?

1 answer

✓ Accepted answer

If you are saying that you want to instantly make the character look to the left or to the right,without the in between turning,then there is a much more simple way of doing it without GoRight() or GoLeft().

I have worked on 2D games too and this can be easily fixed with a simple thing.

Thought I do have a warning... Your game must be situated on the x axis.If your game is not situated on the X axis then this will not be able to turn you in the direction wanted.

UIS.InputBegan:Connect(function(input,IsTyping)
	if input.KeyCode == Enum.KeyCode.Left then 
		Player.Character.HumanoidRootPart.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position,Vector3.new(-900000000,0,0))
--Pressing Left makes a CFrame that has the lookTo to a massive -x number,same goes for Right but this time its a positive x number
	elseif input.KeyCode == Enum.KeyCode.Right then 
		Player.Character.HumanoidRootPart.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position,Vector3.new(900000000,0,0))
	end
end)

--If you somehow have a distance larger than the x number,you might need to make it somehow larger.

1 comment

0

thank you c=

This discussion is preserved as it was. New answers, comments, and votes are closed.