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

How to make variable recognised outside while loop if in a one (LocalScript)(closed)?

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

1 answer

✓ Accepted answer

I would suggest using a place holder variable at the top of the script.

Example,

local x = 0-- Holder variable

spawn(function()
	while wait(1) do
		x = x+1
	end
end)

wait(2)
print(x)
wait(2)
print(x)

The second thing to note is that code will not run after the while loop in your code. It never breaks out of the loop unless VehicleConnected equals false. Maybe you understand this. If you don't want this to be the case, the simplest solution might be to do what I did in my example, and encasing your code in a spawned function.

Good Luck!

If I helped, please don't forget to accept my answer.

2 comments

0

Sorry but I don't quite understand how your solution works (I only know that much of scripting as you can see above). What does holder variable do? How a spawned function works? Maybe you can suggest a better way of getting a variable of the model that has a seat inside on which the current player is sitting on?

0

The holder variable is nothing special - he is just using the name 'holder variable' in context with your question. Basically, 'local' variables only work on their 'tier' and anything within it. This is the reason that vehicle is not defined outside of the if statement, because it is created as local, and is only available inside that if statement.

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