I am attempting to make a variable that would link to a specific object (model in this case) in workspace so that player would be able to control it through local script (needed to use it to acces keyboard input).
So far I came up with this code:
local VehicleConnected = false -- variable to stop while loop from running when not necessary
while VehicleConnected == false do
wait(1)
local player = game.Players.LocalPlayer -- getting player in who the script is
for _, object in pairs(game.Workspace:GetChildren()) do -- searching for model
if object.Name == "Model" then -- model name is object so if name match then continue with following
if object.Seat.Occupant.Parent.Name == player.Name then -- model has a seat as its child so I can use occupant propertie of it
local vehicle = object -- variable I need to be recognised outside this loop!!!
print ("Player connected to the vehicle!") -- debuging
VehicleConnected = true
end
end
end
end
If I try to use variable vehicle outside the while loop it doesn't seem to be recognised (Has a blue underline) but I need it to be so I wouldn't have to do: for_,object in pairs(game.W... when I want that current model.
Can anyone please suggest me how to make this work? If there is no way to do this maybe there is another similar way to accomplish my goal?
Sorry for bad english if any.

1 comment
http://wiki.roblox.com/index.php?title=Scope