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

Leaderstats string value automatically change?

Asked by
Sxerks3 65
8 years ago

So, I'm having trouble getting a leaderstats string value to change automatically after an IntValue changes in correspondence to this. How would I go about doing this?

I have:

datastore = game:GetService("DataStoreService"):GetDataStore("rating")

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"
    local rating = Instance.new("IntValue", leaderstats)
    rating.Name = "Rating"
    local ranking = Instance.new("StringValue", leaderstats)
    ranking.Name = "Rank"

    local key = "user_"..player.userId
    if (datastore:GetAsync(key) ~= 0) then
        rating.Value = datastore:GetAsync(key)
    else
        rating.Value = 1500
    end

    rating.Changed:connect(function(val)
        datastore:SetAsync(key, val)
    end)

    if (rating.Value <= 1500) then
        ranking.Value = "Rust I"
    elseif (rating.Value > 1500 and rating.Value <= 2000) then
        ranking.Value = "Rust II"
    elseif (rating.Value > 2000 and rating.Value <= 2500) then
        ranking.Value = "Rust III"
    end
end)
0
Ooh, yeah, and the datastore itself is working fine. Sxerks3 65 — 8y

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

You aren't setting ranking.Value inside of the Changed connection. You put that part after it, so it only gets set once. To solve that, put it inside the Changed event and set the initial value after the event is connected.

datastore = game:GetService("DataStoreService"):GetDataStore("rating")

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"
    local rating = Instance.new("IntValue", leaderstats)
    rating.Name = "Rating"
    local ranking = Instance.new("StringValue", leaderstats)
    ranking.Name = "Rank"

    local key = "user_"..player.userId

    rating.Changed:connect(function(val)
        datastore:SetAsync(key, val)
        if (rating.Value <= 1500) then
            ranking.Value = "Rust I"
        elseif (rating.Value > 1500 and rating.Value <= 2000) then
            ranking.Value = "Rust II"
        elseif (rating.Value > 2000 and rating.Value <= 2500) then
            ranking.Value = "Rust III"
        end
    end)

    local init=datastore:GetAsync(key)
    if (init ~= 0) then
        rating.Value = init
    else
        rating.Value = 1500
    end
end)
Ad

Answer this question