Answered by
7 years ago Edited 7 years ago
I don't think Global variables are necessary, just use a NumberValue in your player named Lanes to keep track.
01 | local rs = game:GetService( "RunService" ) |
02 | local UIS = game:GetService( "UserInputService" ) |
03 | local Character = script.Parent |
04 | local player = game.Players:GetPlayerFromCharacter(Character) |
06 | local lane = player:WaitForChild( "Lane" ) |
07 | local humanoid = Character:WaitForChild( "Humanoid" ) |
08 | local CityPath = workspace:WaitForChild( "CityPath" ) |
10 | CityPath:WaitForChild( 'Lane1' ), |
11 | CityPath:WaitForChild( 'Lane2' ), |
12 | CityPath:WaitForChild( 'Lane3' ) } |
15 | local max_lanes = #lanes |
17 | UIS.InputBegan:Connect( function (key) |
18 | local next_lane = lane.Value |
19 | if key.KeyCode = = Enum.KeyCode.D then |
21 | next_lane = (next_lane + 1 > max_lanes and 1 or next_lane + 1 ) |
22 | elseif key.KeyCode = = Enum.KeyCode.A then |
24 | next_lane = (next_lane - 1 < 1 and max_lanes or next_lane - 1 ) |
26 | lane.Value = next_lane |
29 | lane.Changed:Connect( function (prop) |
30 | print (lanes [ lane.Value ] ) |
31 | Character:MoveTo(lanes [ lane.Value ] .Position) |