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 3 years ago
Edited 3 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:

01for _, seat in pairs(bus.Bus.seats:GetChildren()) do
02 
03    local Seat = seat.Seat
04    local occupant = Instance.new("StringValue", Seat)
05    occupant.Name = "CurrentOccupant"
06 
07    Seat.Changed:Connect(function()
08        if not Seat.Occupant and not Seat.Disabled then
09            Seat.Disabled = true
10            local plr = occupant.Value
11            local foundPlayer = false
12 
13            if game:GetService("Workspace"):FindFirstChild(plr) then
14                foundPlayer = true
15                plr = game:GetService("Workspace")[plr]
View all 29 lines...

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 3 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