My if statement is broken, how do i fix it?

Hello everyone! I am a beginner scripter, i have come in an error which is very confusing for me, i don’t know if it’s a bug in roblox studio or i am doing something wrong, but this is the if statement:

while task.wait() do	
	if minutes.Value < 5 then -- This is the bug, it fires when i have more than five minutes.
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end

I have like 10 minutes, and it’s firing…
image

I tried to change the if statement many times, but nothing worked. :sad:

So i want people to help me in this, my goal is to have a success in my hangout game. :smile:

3 Likes

Because < is a less than operator, so it would be checking if the players minute value is smaller than 5, instead of larger. Try this;

while task.wait() do	
	if minutes.Value >= 5 then
-- add your code to buy the cola
else
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end

I’d also recommend you read this!

4 Likes

Seems like it will work, let me try it now :smile:

2 Likes

while task.wait(1) do	
	if minutes.Value <= 5 then
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end

<= checks for if the given condition is equal to or less then.

1 Like

also if you wanted, here is my full LocalScript, because there’s more info in it:

local isEquipped = false
local Frame = script.Parent.Frame
local Enter = workspace:WaitForChild("EnterShop")
local EquipButton = script.Parent.Frame.item1.TextButton

Enter.Touched:Connect(function()
	Frame.Visible = true
end)

Enter.TouchEnded:Connect(function()
	Frame.Visible = false
end)

local player = game.Players.LocalPlayer

local minutes = player.leaderstats.Minutes

EquipButton.MouseButton1Down:Connect(function()
	if isEquipped == false and minutes.Value >= 5 and EquipButton.Text == "Equip" then
		
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local Item1 = ReplicatedStorage.BloxyCola
		local ClonedItem1 = Item1:Clone()

		ClonedItem1.Parent = player.Backpack
		
		isEquipped = true

		EquipButton.Text = "UnEquip"
		EquipButton.BackgroundColor3 = Color3.fromRGB(255, 98, 98)
		
	elseif isEquipped == true and EquipButton.Text == "UnEquip" then
		EquipButton.Text = "Equip"
		
		local cloneditem1 = player.Backpack:WaitForChild("BloxyCola")
		cloneditem1:Destroy()
		
		isEquipped = false
		EquipButton.BackgroundColor3 = Color3.fromRGB(109, 255, 101)
	end
end)
	
while task.wait() do	
	if minutes.Value < 5 then
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end
1 Like

I tried that code but it still says “NOT ENOUGH MINUTES”

 while task.wait() do	
	if minutes.Value >= 5 then
		EquipButton.MouseButton1Down:Connect(function()
			if isEquipped == false and EquipButton.Text == "Equip" then

				local ReplicatedStorage = game:GetService("ReplicatedStorage")
				local Item1 = ReplicatedStorage.BloxyCola
				local ClonedItem1 = Item1:Clone()

				ClonedItem1.Parent = player.Backpack

				isEquipped = true

				print("test")

				EquipButton.Text = "UnEquip"
				EquipButton.BackgroundColor3 = Color3.fromRGB(255, 98, 98)

				print("test 2")

			elseif isEquipped == true and EquipButton.Text == "UnEquip" then
				EquipButton.Text = "Equip"

				local cloneditem1 = player.Backpack:WaitForChild("BloxyCola")
				cloneditem1:Destroy()

				isEquipped = false
				EquipButton.BackgroundColor3 = Color3.fromRGB(109, 255, 101)
			end
		end)
		
	else
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end

debbuging, debbuging and debbuging.

print(minutes.Value >= 5, minues)

is console clear? Also tip, put all this loop in a theard, using coroutine.create of task.spawn(f: function)

task.spawn(function 
       while task.wait() do	
	if minutes.Value >= 5 then
		EquipButton.MouseButton1Down:Connect(function()
			if isEquipped == false and EquipButton.Text == "Equip" then

				local ReplicatedStorage = game:GetService("ReplicatedStorage")
				local Item1 = ReplicatedStorage.BloxyCola
				local ClonedItem1 = Item1:Clone()

				ClonedItem1.Parent = player.Backpack

				isEquipped = true

				print("test")

				EquipButton.Text = "UnEquip"
				EquipButton.BackgroundColor3 = Color3.fromRGB(255, 98, 98)

				print("test 2")

			elseif isEquipped == true and EquipButton.Text == "UnEquip" then
				EquipButton.Text = "Equip"

				local cloneditem1 = player.Backpack:WaitForChild("BloxyCola")
				cloneditem1:Destroy()

				isEquipped = false
				EquipButton.BackgroundColor3 = Color3.fromRGB(109, 255, 101)
			end
		end)
		
	else
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end
end)

Btw, instead of using a while loop, use Changed, like this:

minutes.Changed:Connect(function(val)
    if val >= 5 then
        -- code to buy the cola
    else
        EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
        EquipButton.Text = "NOT ENOUGH MINUTES"
    end
end)

This won’t fix your issue but it’s probably better.

Also, I don’t know for sure, but try task.wait(0.1) as using it as 0 could cause exhaustion??

task.wait() should work fine.

3 Likes

And yeah, as mentioned before, debug.

2 Likes

Yes, I wanna say abt this. While loops make load for the server.

1 Like

I made a place file for you that should help guide you into finding a solution towards your problem: BloxyColaExample.rbxl (60.6 KB)

For anyone interested this is the server Script inside of ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local toolData = require(ReplicatedStorage.ToolData)

local remoteFunction = ReplicatedStorage.RemoteFunction

local tools = ServerStorage.Tools

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local minutes = Instance.new("IntValue")
	minutes.Name = "Minutes"
	minutes.Parent = leaderstats

	task.defer(function()
		while true do
			task.wait(60)

			minutes.Value += 1
		end
	end)
end

local function onServerInvoke(player, toolName)
	if typeof(toolName) == "string" and toolData[toolName] then
		local minutes = player.leaderstats.Minutes

		if minutes.Value >= toolData[toolName].MinutesToBuy then
			--minutes.Value -= toolData[toolName].MinutesToBuy (Uncomment this line if you'd like to remove minutes on successfull purchase)

			tools[toolName]:Clone().Parent = player.Backpack

			return true
		end
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)

remoteFunction.OnServerInvoke = onServerInvoke

The LocalScript inside of the TextButton:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local toolData = require(ReplicatedStorage:WaitForChild("ToolData"))

local remoteFunction = ReplicatedStorage.RemoteFunction

local textButton = script.Parent
textButton.Text = string.format("BUY %s (COST: %i MIN)", string.upper(textButton.Name), toolData[textButton.Name].MinutesToBuy)

local minutes = Players.LocalPlayer.leaderstats.Minutes

local function onActivated()
	if minutes.Value >= toolData[textButton.Name].MinutesToBuy
	and remoteFunction:InvokeServer(textButton.Name)
	then
		textButton.BackgroundColor3 = Color3.new(0, 1, 0)
		textButton.Text = "SUCCESS"
	else
		textButton.BackgroundColor3 = Color3.new(1, 0, 0)
		textButton.Text = "NOT ENOUGH MINUTES"
	end

	task.wait(1)

	textButton.BackgroundColor3 = Color3.new(0, 0, 0)
	textButton.Text = string.format("BUY %s (COST: %i MIN)", string.upper(textButton.Name), toolData[textButton.Name].MinutesToBuy)
end

textButton.Activated:Connect(onActivated)

And the ToolData module:

return {
	["Bloxy Cola"] = {MinutesToBuy = 5}
}

Edit: @KCapre_Dev As a precaution I recommend within the LocalScript in the place file I gave you replacing:

local remoteFunction = ReplicatedStorage.RemoteFunction

With:

local remoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")

And replacing:

local minutes = Players.LocalPlayer.leaderstats.Minutes

With:

local minutes = Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Minutes")

Just in case the Instances aren’t loaded in time for the player

while true do task.wait(0.33)
	if minutes.Value >= 5 then
		EquipButton.MouseButton1Down:Connect(function()
			if isEquipped == false and EquipButton.Text == "Equip" then
				local ReplicatedStorage = game:GetService("ReplicatedStorage")
				local Item1 = ReplicatedStorage.BloxyCola
				local ClonedItem1 = Item1:Clone()
				ClonedItem1.Parent = player.Backpack
				isEquipped = true
				EquipButton.Text = "UnEquip"
				EquipButton.BackgroundColor3 = Color3.fromRGB(255, 98, 98)
			elseif isEquipped == true and EquipButton.Text == "UnEquip" then
				EquipButton.Text = "Equip"
				player.Backpack:WaitForChild("BloxyCola"):Destroy()
				isEquipped = false
				EquipButton.BackgroundColor3 = Color3.fromRGB(109, 255, 101)
			end
		end)
	else
		EquipButton.BackgroundColor3 = Color3.fromRGB(168, 55, 55)
		EquipButton.Text = "NOT ENOUGH MINUTES"
	end
end