Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
Edited 2 years ago

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.

1 answer

Log in to vote
0
Answered by 2 years ago

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.

Ad

Answer this question