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

Why is it saving the stat in the wrong place?

Asked by
LawlR 182
6 years ago
Edited 6 years ago

In the main game/spawn place, when a player joins, it creates a model inside the player, and then a string value inside that model. The model is not called leaderstats, so it does not make a leaderboard. This string value saves and loads when the player leaves/enters and it works.

However, when I teleport to a place within the main game, the same model gets created, but along with another one called leaderstats, with an int value inside it and this does create a leaderboard. But when I leave the place, it saves the leaderboard value as the string value, and doesn't save the leaderboard value. So when I enter the main game again, the string value is set to whatever the leaderboard value was in the other place. How would I fix this, if it's even possible?

Here is the script from the main game:

local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")

game.Players.PlayerAdded:Connect(function(plr)
    local charnum = Instance.new("Model",plr)
    charnum.Name = "CharacterStat"
    local value = Instance.new("StringValue",charnum)
    value.Name = "Character"
    value.Value = "(NEW PLAYER)"

    local key = "user "..plr.userId
    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        charnum.Character.Value = savedValues[1]

    else
        local valuestosave = {charnum.Character.Value}
        DataStore:SetAsync(key, valuestosave)
    end

    game.Players.PlayerRemoving:connect(function(plr2)
    local key = "user "..plr2.userId    
    local valuestosave = {plr2:FindFirstChild("CharacterStat").Character.Value}
    DataStore:SetAsync(key, valuestosave)
end)
end)

Here is the script from the second place:

local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")


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

    local charnum = Instance.new("Model",plr)
    charnum.Name = "CharacterStat"
    local value = Instance.new("StringValue",charnum)
    value.Name = "Character"
    value.Value = "(NEW PLAYER)"

    local testnum = Instance.new("Model",plr)
    testnum.Name = "leaderstats"
    local testvalue = Instance.new("IntValue",testnum)
    testvalue.Name = "Test Value"


    local key = "user "..plr.userId
    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        charnum.Character.Value = savedValues[1]
        testnum["Test Value"].Value = savedValues[2]
    else
        local valuestosave = {charnum.Character.Value, testnum["Test Value"].Value}
        DataStore:SetAsync(key, valuestosave)
    end

end)

--

game.Players.PlayerRemoving:connect(function(plr2)

    local key = "user "..plr2.userId    

    local valuestosave = {plr2.leaderstats["Test Value"].Value, plr2.CharacterStat.Character.Value}
    DataStore:SetAsync(key, valuestosave)

end)

Answer this question