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

DataStore wont work properly?

Asked by 5 years ago

A am trying to save a leaderstat called "coins" but it wont save and it makes two leaderstats one named "Coins" and one named "coins" with a lowercase c.

game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("IntValue",plr)
    stats.Name = "leaderstats"
    local coins = Instance.new("IntValue",stats)
    coins.Name = "Coins"
end)
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CoinSaveSystem")

game.Players.PlayerAdded:connect(function(player)
 local stats = Instance.new("Folder",player)
 stats.Name = "leaderstats"
 local coins = Instance.new("IntValue",stats)
coins.Name = "coins"
 coins.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, coins.Value)
 coins.Changed:connect(function()
  ds:SetAsync(player.UserId, coins.Value)
 end)
end)


game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.coins.Value)
end)
0
Need more info on what you mean by "it wont save". Do you mean that the value is created but left with a nil value, or just that it's creating two datastore values? nick540 20 — 5y
0
I mean for example if I got 50 coins and leave when I come back it is back to zero coins Prbo1201 56 — 5y
0
there are always two lederststs Prbo1201 56 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local dataStore = game:GetService("DataStoreService"):GetDataStore("CoinStorage")

local function autoSave()
    -- this is a much safer way of saving
    -- btw this checks if the player count is above 0
    if #Players:GetPlayers() > 0 then
        for _, player in next, Players:GetPlayers() do
            dataStore:SetAsync(player.UserId,           player:WaitForChild("leaderstats"):WaitForChild("Coins").Value)
        end
    end

    wait(120) -- go to roblox wiki and change it according to the times shown on that page
    autoSave()
end

-- wraps the call of autosave in a function so it doesn't go for more then twice amounts of time
local recursiveCall = coroutine.wrap(autoSave)
recursiveCall()

-- run this event only once, not twice
Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("Folder", player)
    stats.Name = "leaderstats"
    -- changed it to a number value so if you are using decimals the value can still be usable
    local coins = Instance.new("NumberValue", stats)
    coins.Name = "Coins"
    coins.Value = dataStore:GetAsync(player.UserId) or 0

    --[[ I took out your changed event due to that being easily exploited to hackers. They can just change the value of their coins,
    and there it goes, it saves right there. Also if the "coins" value is changing rapidly, the connection to the data store wiil be
    throtlled at some point, and that's another reason why I took that out as well. --]]
end)

Players.PlayerRemoving:Connect(function(player)
    dataStore:SetAsync(player.UserId, player:WaitForChild("leaderstats"):WaitForChild("Coins").Value)
end)

I suggest changing the name of the Data Store as well. If you have a second script for this, delete it. If you have more questions about it, make it one or comment on this.

0
earning points works but it dosent save this is an error on line 37: 15:57:15.967 - 104: Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters. Prbo1201 56 — 5y
0
Put .Value at the end of "WaitForChild("Coins"). FireyMcBlox 134 — 5y
0
ok I will try tomorrow Prbo1201 56 — 5y
0
omg thx sooooooo much Prbo1201 56 — 5y
Ad
Log in to vote
0
Answered by
nick540 20
5 years ago
Edited 5 years ago

The reason why the datastore isn't behaving like you were expecting is due to a typo on line 14, where you put coins.Name = "coins" instead of coins.Name = "Coins". The script provided works perfectly on its own. However, it would appear that you have another script that is adding the value Coins to the leaderstats folder. This script is only monitoring coins, not Coins. So if you set Coins to 50, and leave coins at 0, the value saved will be the 0 stored in coins while Coins is ignored.

As for the problem of the other leaderstat; I can't replicate it from this script, suggesting that its a value generated from another script. (No, its not lines 1 - 6, while redundant, they don't cause the problem on their own) To fix that problem, check to see if any of your other scripts are creating new values in the leaderstats folder.

Answer this question