First if statement doesn't work, only the elseifs after

I’m trying to make an inventory system, where if an inventory value changes, a text button turns visible. The first and second elseif statements work, but for some reason the first if statement doesn’t work. Local script in frame.

local player = game.Players.LocalPlayer
local checkedRainbow = false
local checkedRed = false
local checkedGrey = false

local rs = game:GetService("RunService")

rs.Heartbeat:Connect(function(dt)
	if player.Trails.RainbowTrail.Value > 0 and checkedRainbow == false then
		checkedRainbow = true
		player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.RainbowTrail.Visible = true
	elseif player.Trails.GreyTrail.Value > 0 and checkedGrey == false then
		checkedGrey = true
		player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.GreyTrail.Visible = true
	elseif player.Trails.RedTrail.Value > 0 then
		checkedRed = true
		player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.RedTrail.Visible = true
	end
end)```

Hey!

I’ve faced similar problems when I started programming, have you tried using print before your IF statement to verify if both values are indeed truthy?

1 Like

Hello, thanks for the reply, they are, I’m still not sure why it wont work.

I’m not sure I completely understand the system you’re trying to make. From what I can tell, it looks like each one of these are independent conditions. In other words, whether the RainbowTrail button appears is completely independent from the GreyTrail and/or the RedTrail, etc. Therefore, you probably don’t want to use elseif at all. Maybe something as simple as this will work:

local guiObject = player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.RainbowTrail
local vTrail = player.Trails.RainbowTrail
guiObject.Visible = vTrail.Value > 0

In fact, it looks like you want to use this observer pattern (update once, then on changed event fire) instead of updating on a loop/every frame

-- The objects we care about
local vTrail = player.Trails.RainbowTrail
local guiObject = player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.RainbowTrail

-- This function's job is to make sure the UI reflects your game state.
local function update()
   guiObject.Visible = vTrail.Value > 0
end

-- Update once, and every time the value changes
update()
vTrail.Changed:Connect(update)
1 Like

Ah, thank you so much for the reply, all is working now.

local player = game.Players.LocalPlayer
local checkedRainbow = false
local checkedRed = false
local checkedGrey = false

player.Trails.RainbowTrail.Changed:Connect(function(newVal)
	if newVal > 0 and checkedRainbow == false then
		checkedRainbow = true
		player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.RainbowTrail.Visible = true
	end
end)

player.Trails.GreyTrail.Changed:Connect(function(newVal)
	if newVal > 0 and checkedGrey == false then
		checkedGrey = true
		player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.GreyTrail.Visible = true
	end
end)

player.Trails.RedTrail.Changed:Connect(function(newVal)
	if newVal > 0 and checkedRed == false then
		checkedRed = true
		player.PlayerGui.MainUI.Shop.EffectsFrame.TrailInventoryFrame.RedTrail.Visible = true
	end
end)