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

My value does not print " - " when i decrease value ?

In the console when i decrease value it just showing the " + " Console : > game.Players.tommy2100.leaderstats.Money.Value = game.Players.tommy2100.leaderstats.Money.Value - 250 Note: I have create a LocalScript and put into StarterGui

local plr = game.Players.LocalPlayer

local stats = plr:WaitForChild("leaderstats")

local money = stats:WaitForChild("Money")

local gold = stats:WaitForChild("Gold")

local rs = game:GetService("ReplicatedStorage")

local sound = rs:WaitForChild("GetCash")

local sound2 = rs:WaitForChild("GetGold")

  

money.Changed:Connect(function()

if money.Value + money.Value then

print("+")

elseif money.Value - money.Value then

print("-")

end

end)

  

gold.Changed:Connect(function()

if gold.Value + gold.Value then

print("+")

elseif gold.Value - gold.Value then

print("-")

end

end)

2 comments

0

This's the exact same thing as doing 5 + 5; you're adding and subtracting a number, which will return a result (5 + 5 = 10, so 10 will return), so it's going to do if 10 then. The solution's to keep a variable of the last number and compare it.

0

Use gold:GetPropertyChangedSignal("Value"):Connect(function() end)

1 answer

That isnt how 'if statements' work, when you put

if xyz - xyz then dostuff() end

It subtracts xyz from xyz and then checks if that isnt false or nil, if it isnt false or nil then it passes the 'if statement'. So it immeaditly goes through the first 'if statement' and never reaches the elseif.

Instead you should check how the value has changed since it was last set.

xyz1 = 0
xyz2 = xyz1
xyz1 = xyz1 + 1
if xyz1 > xyz2 then
    print("Value is higher!")
elseif xyz1 < xyz2 then
    print("Value is lower")
end

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