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

DataStore isn't working functionally, does anyone know what I'm doing wrong?

Asked by
xNypify 11
5 years ago

I've tried DataStore for awhile now and I'm sill not getting the hang of it, as in it isn't working properly, sometimes it loads the players data and sometimes it doesn't. I was hoping one of y'all could help me. Also I don't know what the rules are for reposting a questions that hasn't been answered in awhile so if that is prohibited, then my bad and I wont continue doing so.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderboardSaveSystem")
switch = false
switch2 = false

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Speed = Instance.new("NumberValue",leader)
 local Rebirth = Instance.new("NumberValue",leader)
 Speed.Name = "Speed"
 Rebirth.Name = "Rebirth"
 Speed.Value = ds:GetAsync(player.UserId) 
 Speed.Value = ds:GetAsync(player.UserId) 
 ds:SetAsync(player.UserId, Speed.Value)
 ds:SetAsync(player.UserId, Rebirth.Value)
 Speed.Changed:connect(function()
    if switch == false then
     switch = true
     print("sanic")
     ds:SetAsync(player.UserId, Speed.Value)
     wait(7)
     switch = false
end
     end)
 Rebirth.Changed:connect(function()
    if switch2 == false then
    switch2 = true
    print("rebirth'd")
    ds:SetAsync(player.UserId, Rebirth.Value)
    wait(7)
    switch2 = false
end
 end)
end)


game.Players.PlayerRemoving:connect(function(player)
 print("player left")
 ds:SetAsync(player.UserId, player.leaderstats.Speed.Value)
 ds:SetAsync(player.UserId, player.leaderstats.Rebirth.Value)
end)

The print functions are just for testing purposes, as they do still print when the function is called, it just doesn't save the data/ update it.

0
connect is deprecated, use Connect. Parent argument to Instance.new is also deprecated, and you are using data stores wrong. User#19524 175 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The problem with your script is in lines 40 and 41.

 ds:SetAsync(player.UserId, player.leaderstats.Speed.Value)
 ds:SetAsync(player.UserId, player.leaderstats.Rebirth.Value)

Scripts read from top to bottom, so line 41 takes precedence, saving the Rebirth, but not the Speed. A fix to this would be to make a separate DataStore for the Speed or Rebirth. HOWEVER, this isn't necessary! You can also save tables, so you'd have the speed and rebirth values in the table.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderboardSaveSystem")
local Players = game:GetService("Players")
local switch = false
local switch2 = false

Players.PlayerAdded:Connect(function(player) -- connect is deprecated use Connect
    local key = tostring(player.UserId) -- should be a string not a number
    local data = ds:GetAsync(key) 
    local leader = Instance.new("Folder") -- parent argument deprecated, assign parent last
    leader.Name = "leaderstats"
    leader.Parent = player -- assign last

    local Speed = Instance.new("NumberValue")
    speed.Name = "Speed"

    local Rebirth = Instance.new("NumberValue")
    Rebirth.Name = "Rebirth"

    local success, msg = pcall(function() -- could throw an error, wrap in pcall
        if data then -- if there's data 
            Speed.Value = data[1]
            Rebirth.Value = data[2]
        else -- no data.
            -- you could give a starter value if you wanted
            Speed.Value = 0 
            Rebirth.Value = 0
        end
    end)
end) 


Players.PlayerRemoving:Connect(function(player)
    local data = { -- table to save. 
        player.leaderstats.Speed.Value,
        player.leaderstats.Rebirth.Value
    }

    local key = tostring(player.UserId)
    print("player left:", player)
    ds:SetAsync(key, data)
end)

Ad
Log in to vote
0
Answered by
valchip 789 Moderation Voter
5 years ago
Edited 5 years ago

Do NOT use :SetAsync() frequently. Only use :SetAsync() only when the player leaves. Also you should use pcalls. A pcall helps you handle an error. You can learn more about pcalls by visting wiki's article http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pcall.

You can use a pcall to handle errors that come from data store for several reasons. For example if a player leaves and his data doesn't save due to an error, you use a pcall to try and save his data.

I found a thread on devforum which will definitely help you:

https://devforum.roblox.com/t/help-with-datastores/72001

Hope this helped you!

-bimorp

Answer this question