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

Help this is suppoppsed to make my player switch lanes but wont work, help? :(

		--@LeafDoode & SilverBunny_Gd

_G.TheLane = script.Lane.Value

local Character = script.Parent
local CityPath = game.Workspace.CityPath
lanes = {game.Workspace.CityPath.Lane1,game.Workspace.CityPath.Lane2,game.Workspace.CityPath.Lane3}
repeat wait() until Character.Humanoid

local UIS = game:GetService("UserInputService")
_G.TheLane = 0
UIS.InputBegan:connect(function(i)
	if i.KeyCode == Enum.KeyCode.D then
		_G.TheLane = _G.TheLane + 1
		_G.Lane1 = lanes[_G.TheLane]
		print(unpack(lanes))
			local	rs = game:GetService("RunService")
rs.Heartbeat:connect(function()
if _G.Lane1 == 1 then
	Character.Humanoid:MoveTo(_G.Lane1.Position)
	elseif _G.Lane == 2 then 
	Character.Humanoid:MoveTo(_G.Lane1.Position)
	elseif _G.Lane == 3 then 
	Character.Humanoid:MoveTo(_G.Lane1.Position)
end
end)
	end
	if i.KeyCode == Enum.KeyCode.A then
		_G.TheLane = _G.TheLane - 1
		_G.Lane1 = lanes[_G.TheLane]
	local	rs = game:GetService("RunService")
rs.Heartbeat:connect(function()
if _G.Lane1 == 1 then
	Character.Humanoid:MoveTo(_G.Lane1.Position)
	elseif _G.Lane == 2 then 
	Character.Humanoid:MoveTo(_G.Lane1.Position)
	elseif _G.Lane == 3 then 
	Character.Humanoid:MoveTo(_G.Lane1.Position)
	end
end)
end 


	end)





1 answer

I don't think Global variables are necessary, just use a NumberValue in your player named Lanes to keep track.

local rs = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Character = script.Parent
local player = game.Players:GetPlayerFromCharacter(Character)
-- Just put a number value in player named Lane
local lane = player:WaitForChild("Lane") --Instance.new("NumberValue", player)
local humanoid = Character:WaitForChild("Humanoid")
local CityPath = workspace:WaitForChild("CityPath")
local lanes = {
	CityPath:WaitForChild('Lane1'),
	CityPath:WaitForChild('Lane2'),
	CityPath:WaitForChild('Lane3')}


local max_lanes = #lanes

UIS.InputBegan:Connect(function(key)
	local next_lane = lane.Value
	if key.KeyCode == Enum.KeyCode.D then
		-- if next_lane + 1 > 3 then next_lane = 1 else next_lane = next_lane + 1
		next_lane = (next_lane + 1 > max_lanes and 1 or next_lane + 1)
	elseif key.KeyCode == Enum.KeyCode.A then 
		-- if next_lane - 1 < 1 then next_lane = 3 else next_lane = next_lane - 1
		next_lane = (next_lane - 1 < 1 and max_lanes or next_lane - 1)
	end
	lane.Value = next_lane
end)

lane.Changed:Connect(function(prop)
	print(lanes[lane.Value])
	Character:MoveTo(lanes[lane.Value].Position)
end)

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