Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to set a max value for a int value?

Asked by
MAKKU 37
5 years ago

So i'm trying to make it so you cant exceed a certain amount of coins. This is what i got so far:

game.Players.PlayerAdded:connect(function(plr)

    local stats = Instance.new("Folder",plr)
    stats.Name = "leaderstats"

    local coins = Instance.new("IntValue",plr.leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    plr.leaderstats.Coins.Changed:Connect(function()
        local max = 10
        if Coins.Value > max then
            Coins.Value = 10
        end

end)
end)

2 answers

Log in to vote
0
Answered by
starmaq 1290 Moderation Voter
5 years ago

This is right, but i think it will be better to make this in the script that is going to give the coins.

local max = 10  -- or anything amount you want
local coins = plr.leaderstats.Coins

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    if coins <= max then
        coins = coins + 1
    else
        -- do nothing
    end
end)

youre script had some issues that i will explain, what you did is, you checked if coins is bigger than max, and if so it will be automaticlly set to 10, thats cool and not wrong but tbh that's just complicating stuff, just make it so it checks if coins are smaller than the max, and do it normally without setting it to 10. also i can tell that this is your frist time using operators, they are always used and ez to play with here https://developer.roblox.com/articles/Operators

0
ty starmaq 1290 — 5y
0
Is there even a need for the else part? OptimisticSide 199 — 5y
0
yah thats probarly uneeded @Exo_Byte starmaq 1290 — 5y
Ad
Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago

In line 12, you are referencing your coins wrongly. It is trying to find a global variable called Coins which does not exist. You should also not repeat yourself when indexing the variables.

In another note, do not use :connect, rather use :Connect since it is deprecated, which means ROBLOX is not supporting it further and could break in later updates.

Also, the parent argument in Instance.new is deprecated because of performance loss when setting the parent first before any other things. When creating an instance, parent it last.

--Use :Connect
game.Players.PlayerAdded:Connect(function(plr)

    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent = plr


    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    --Since you already define stats, you can parent it to stats instead of plr.leaderstats
    coins.Parent = stats

    --Since you already define coins, you can use it instead of reindexing everything.
    coins.Changed:Connect(function()
        local max = 10
        if coins.Value > max then
            coins.Value = 10
            end
    end)
end)

Answer this question