Script elseif seems to not be working

I am trying to make a script that sells items in my game. The scripts seems to work fine when the items are above the MaxNumber. But when they’re below the max number nothing happens, including no errors. Here’s the code to make it make more sense,

local RemoteEvent = game.ReplicatedStorage.ItemSold
local Cash = game.Players.LocalPlayer.Values.Cash
local Item = game.Players.LocalPlayer.Values.Paper
local MaxSold = game.Players.LocalPlayer.Values.RItemMax
local TimeWait = game.Players.LocalPlayer.Values.RItemTime
local CashName = Cash.Name
local SellingFor = 5
local N2Abbreviate = require(game:GetService("ReplicatedStorage").N2Abbreviate_Module)


while true do
	wait(TimeWait.Value)
	if Item.Value == 0 then
		wait(40)
	end
	if Item.Value > 0 then
		if Item.Value >= MaxSold.Value then
			local NumberSold = math.random(1,MaxSold.Value)
			RemoteEvent:FireServer(Item,NumberSold,CashName,SellingFor)
			script.Parent.LastSold.Text = "Last Sold ".. NumberSold.." Sheets of Paper!"
			script.Parent.LastSoldAmount.Text = "+$"..N2Abbreviate.Convert(NumberSold*SellingFor,"Advanced")	
		end
	elseif Item.Value < MaxSold.Value then
		local NumberSold = math.random(1,Item.Value)
		RemoteEvent:FireServer(Item,NumberSold,CashName,SellingFor)
		script.Parent.LastSold.Text = "Last Sold ".. NumberSold.." Sheets of Paper!"
		script.Parent.LastSoldAmount.Text = "+$"..N2Abbreviate.Convert(NumberSold*SellingFor,"Advanced")	
	   end	
	end	

MaxNumber is 20

If MaxSold.Value is positive, then the elseif block can never run, because no number is positive, less than another positive number and less than zero at the same time. You probably meant to have the order like this:

if Item.Value < MaxSold.Value then
--Value is greater than zero and less than MaxSold.Value
elseif Item.Value == 0 then
--Value is zero exactly
elseif Item.Value < 0
--Value is less than zero
end

Essentially for an elseif block in your code to run, not only does the condition have to be true, but every condition in previous elseif or if blocks has to not be true. The order matters in this case.

When your Item.Value is below MaxSold.Value, but it is above zero, it will never get to the elseif statement.

Currently you have this structure:

if Item.Value > 0 then
	if Item.Value >= MaxSold.Value then

    end
elseif Item.Value < MaxSold.Value then
    --The script will never get here if the value is above zero!
end	

But I think you need somehting like this:

if Item.Value > 0 then
	if Item.Value >= MaxSold.Value then

    elseif Item.Value < MaxSold.Value then

    end
end	

I believe that wouldn’t really achieve what OP is trying to do. If Item.Value is equal to zero, their script waits 40 seconds before doing the other checks (checking if it’s less or greather than MaxSold.Value).
So the Item.Value == 0 statement has to be checked separately before the others, not using an elseif.

That’s only because wait() has a side effect (waiting), interestingly. I didn’t want to assume anything about what the script actually does.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.