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

[SOLVED] Why is it acting like a while loop?

So I am currently making this script for someone and there is something wrong Whenever the .Changed event gets fired it Basically automaticly puts everything inside the event into a while loop and the event repeats itself forever.

Why is this please help

Code:

for _, seat in pairs(bus.Bus.seats:GetChildren()) do
	
	local Seat = seat.Seat
	local occupant = Instance.new("StringValue", Seat)
	occupant.Name = "CurrentOccupant"
	
	Seat.Changed:Connect(function()
		if not Seat.Occupant and not Seat.Disabled then
			Seat.Disabled = true
			local plr = occupant.Value
			local foundPlayer = false
			
			if game:GetService("Workspace"):FindFirstChild(plr) then
				foundPlayer = true
				plr = game:GetService("Workspace")[plr]
			end
			
			if not foundPlayer then
				print(plr, "left the game")	
			else
				repeat wait() until not plr.Humanoid.Sit
				plr.HumanoidRootPart.Position = bus.teleportPos.Position
			end
			Seat.Disabled = false
			
		end
	end)
	
end

I found out that the reason if fires al the time is because th event changes Seat.Disabled, firing the event again.

2 comments

0

if its nil

1 answer

Instead of checking if the humanoid is sitting on line 21 I'd check if seat.Occupant ~= nil. Also seat.Changed is gonna fire every single time any property of the seat is changed, and that includes throttle and steering. You can pass in property as the parameter in the function, so you can use it to specify which property triggers the rest of the code to execute.

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