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

Why does nothing save When the player leaves?

Asked by 5 years ago
Edited 5 years ago

it loads but nothing saves when I'm in a server


local main = require(game.ServerScriptService.Main) game.Players.PlayerAdded:Connect(function(player) main.GiveStats(player) end) game.Players.PlayerRemoving:Connect(function(player) main.SaveStats(player) end)

Module script:

function Main.GiveStats(player)
    local datastore = game:GetService("DataStoreService")
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local Ores = Instance.new("Folder", player)
    Ores.Name = "Ores"
    local points = Instance.new("IntValue", leaderstats) 
    points.Name = "Cash" 
    local Pickaxes = Instance.new("Folder", player)
    Pickaxes.Name = "Pickaxes"
    local key = player.userId
    local Blacklist = Instance.new("Folder", player)
    Blacklist.Name = "Blacklist"
    local ls = datastore:GetOrderedDataStore("CashSaveSystem"):GetAsync(key)
    for i = 1, #Main.Ores do
        if datastore:GetOrderedDataStore(Main.Ores[i]):GetAsync(key) then
            local Item = Instance.new("IntValue", Ores)
        Item.Name = Main.Ores[i]
        Item.Value = datastore:GetOrderedDataStore(Main.Ores[i]):GetAsync(key)
        end
    end
    if ls then
        points.Value = ls
    end 
    Main.GivePlayerTycoon(player)
end
function Main.SaveStats(plr)
    local key = plr.UserId
    local ds = game:GetService("DataStoreService")
    for i,v in pairs(plr.Ores:GetChildren()) do
        ds:GetOrderedDataStore(v.Name):SetAsync(key, v.Value)
    end
    local ls = plr.leaderstats.Cash.Value 
    ds:SetAsync(key, ls)
end
0
If you provided the module script, it would help a lot. User#19524 175 — 5y
0
theres alot of stuff but ok mattchew1010 396 — 5y
0
done mattchew1010 396 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Your key variable is an int64 of the player's UserId. The key needs to be a string. Also, on line 34, you need to save the value as a table. Here's your fixed code:

local Main = {}

function Main.GiveStats(player)
    local datastore = game:GetService("DataStoreService")
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Ores = Instance.new("Folder")
    Ores.Name = "Ores"
    Ores.Parent = player -- Parent argument to instance.new is deprecated, parent in another line

    local points = Instance.new("IntValue") 
    points.Name = "Cash"  
    pointe.Parent = leaderstats
    local Pickaxes = Instance.new("Folder")
    Pickaxes.Name = "Pickaxes"
    Pickaxes.Parent = player

    local key = "Player_".. player.UserId

    local Blacklist = Instance.new("Folder")
    Blacklist.Name = "Blacklist"
    Blacklist.Parent = player

    local ls = datastore:GetOrderedDataStore("CashSaveSystem"):GetAsync(key)

    for i = 1, #Main.Ores do
         local oresData = datastore:GetOrderedDataStore(Main.Ores[i]):GetAsync(key) 

        if oresData then
            local Item = Instance.new("IntValue", Ores)
            Item.Name = Main.Ores[i]
            Item.Value = oresData[1]
        end
    end

    if ls then
        points.Value = ls[1] -- need to index like this
    end 

    Main.GivePlayerTycoon(player)
end
function Main.SaveStats(plr)
    local key = "Player_".. plr.UserId -- needs to be a string. my key is an example. 
    local ds = game:GetService("DataStoreService")

    for i,v in pairs(plr.Ores:GetChildren()) do
        ds:GetOrderedDataStore(v.Name):SetAsync(key, {v.Value})
    end

    local ls = plr.leaderstats.Cash.Value 
    ds:SetAsync(key, {ls}) -- the saving problem was here. make it a table
end

return Main
0
thanks mattchew1010 396 — 5y
0
i have a question though is 1 suppossed to be i at line 34 mattchew1010 396 — 5y
0
actually do you even need that part? mattchew1010 396 — 5y
Ad

Answer this question