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

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

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

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

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

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