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

After i rejoin game all my values are 0?

Asked by
imKlevi 94
4 years ago

After i rejoin my game, all my values go 0, please help me.

local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("FruitSimulatorDataStore02")

game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("Folder", game.ReplicatedStorage)
    stats.Name = plr.Name.."Stats"
    local coins = Instance.new("NumberValue",stats)
    coins.Name = "Coins"
    coins.Value = data:GetAsync(plr.UserId) or 0
    local baglvl = Instance.new("NumberValue",stats)
    baglvl.Name = "Bag"
    baglvl.Value = data:GetAsync(plr.UserId) or 15
    local fruits = Instance.new("NumberValue", stats)
    fruits.Name = "Fruits Collected"
    fruits.Value = data:GetAsync(plr.UserId) or 0
    local world2unlocked = Instance.new("NumberValue",stats)
    world2unlocked.Name = "W2Unlocked"
    world2unlocked.Value = data:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(player)
    data:SetAsync(player.UserId, game.ReplicatedStorage[player.Name.."Stats"].Coins.Value, game.ReplicatedStorage[player.Name.."Stats"]["Fruits Collected"].Value, game.ReplicatedStorage[player.Name.."Stats"].Bag.Value, game.ReplicatedStorage[player.Name.."Stats"].W2Unlocked.Value)
    print("Saved "..player.Name.."'s Stats.")
end)
0
I dont know if this would even work or make it better but try making the values save into a folder that is added into the player when they join containing all of their stats. Im not to good with save scripts so i dont know much about them so this is what i though of> adieking1 69 — 4y
0
Because you have placed the folder in game.ReplicatedStorage adieking1 69 — 4y
0
@adieking1 The reason is not because it is in the ReplicatedStorage. qVoided 221 — 4y
0
@imKlevi FYI the parent argument for Instance.new is deprecated. Just use instance.Parent = Parent qVoided 221 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Your script looks fine; however, I would advise you to save your values in a table as there is a limited request count with saving multiple values in one data store. Now, I will address what I think is your problem. Try adding the :BindToClose function to your script because there are times when the server shuts down before the last player's data is saved. This could also happen while you're playing solo in the Studio. :BindToClose will allow all your remaining functions (including PlayerRemoving) to run less than 30 seconds before the server shuts down. Simply add this script to yours at the end:

game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do -- loops through all the players in the server

    local values = {
game.ReplicatedStorage[player.Name.."Stats"].Coins.Value,                       game.ReplicatedStorage[player.Name.."Stats"]["Fruits Collected"].Value,     game.ReplicatedStorage[player.Name.."Stats"].Bag.Value,     game.ReplicatedStorage[player.Name.."Stats"].W2Unlocked.Value
}

            data:SetAsync(player.UserId, values)
        print("Saved "..player.Name.."'s Stats.")

    end
end)
0
I should use BindToClose instead of PlayerLeaving ? imKlevi 94 — 4y
0
Not working. imKlevi 94 — 4y
0
I told you to add this script to your original. Don't remove anything from your original script. AntiWorldliness 868 — 4y
0
Okay imKlevi 94 — 4y
0
Yeah but bag value before save it's 15, after save it goes same as "Fruits Collected" value. imKlevi 94 — 4y
Ad
Log in to vote
0
Answered by
iWasThisi -15
4 years ago

So I Have Made A Model And I Gave This To Someone Before, Just Change The Values To Yours And It Should Save!

If This Helps or Theres A Problem Tell Me

Link: https://www.roblox.com/library/5061331155/DataStore-SAVING-LEADERSTATS

Answer this question