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

.Changed:Connect() does not react on a change! How to fix?

Asked by 2 years ago

I'm making a fuel system and I want the car to stop when the fuel runs out. But .Changed:Connect() doesn't work! How can I fix this?

My Code:

if script.Parent.Parent.Name == "Plugins" then return end

local player = script.Parent.Parent.Parent.Parent
local playerName = player.Name
local car = game.Workspace["Car"..playerName]

local MaxAmount = car.Fuel.MaxFuel.Value

local DecreaseFrequency = car.Fuel.DecreaseFrequency.Value
local DecreaseAmount = car.Fuel.DecreaseAmount.Value

local fuel = car.Fuel

local Formula = math.clamp(fuel.Value/MaxAmount, 0, 1)
script.Parent.Bar:TweenSize(UDim2.new(Formula, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15, true)

while true do
    wait(DecreaseFrequency)

    if fuel.Value >= 1 then

        fuel.Value -= DecreaseAmount

    end

end

fuel.Changed:Connect(function(NewValue)
    print(NewValue)

    print("FUEL!!!")

    local Formula = math.clamp(fuel.Value/MaxAmount, 0, 1)
    script.Parent.Bar:TweenSize(UDim2.new(Formula, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15, true)

    if fuel.Value <= 0 then
        fuel.Value = 0

        script.Parent.Bar.Visible = false
        player.PlayerGui["A-Chassis Interface"].IsOn.Value = false
    else
        script.Parent.Bar.Visible = true
        player.PlayerGui["A-Chassis Interface"].IsOn.Value = true
    end
end)

2 answers

Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

That's because theres a while loop above it, place the while loop below it for it to run.

if script.Parent.Parent.Name == "Plugins" then return end

local player = script.Parent.Parent.Parent.Parent
local playerName = player.Name
local car = game.Workspace["Car"..playerName]

local MaxAmount = car.Fuel.MaxFuel.Value

local DecreaseFrequency = car.Fuel.DecreaseFrequency.Value
local DecreaseAmount = car.Fuel.DecreaseAmount.Value

local fuel = car.Fuel

local Formula = math.clamp(fuel.Value/MaxAmount, 0, 1)
script.Parent.Bar:TweenSize(UDim2.new(Formula, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15, true)

fuel.Changed:Connect(function(NewValue)
    print(NewValue)

    print("FUEL!!!")

    local Formula = math.clamp(fuel.Value/MaxAmount, 0, 1)
    script.Parent.Bar:TweenSize(UDim2.new(Formula, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15, true)

    if fuel.Value <= 0 then
        fuel.Value = 0

        script.Parent.Bar.Visible = false
        player.PlayerGui["A-Chassis Interface"].IsOn.Value = false
    else
        script.Parent.Bar.Visible = true
        player.PlayerGui["A-Chassis Interface"].IsOn.Value = true
    end
end)

while true do
    wait(DecreaseFrequency)

    if fuel.Value >= 1 then

        fuel.Value -= DecreaseAmount

    end

end

Answer this question