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

I cant save Data using DataStoreService?

Asked by 5 years ago
Edited 5 years ago

Note:I created that script in ServerScriptService and I allowed API requests,

My script:

local DS = (game:GetService("DataStoreService"):GetDataStore("sjisjsikjawju"))
game.Players.PlayerAdded:connect(function(plr)
local leaderstats = Instance.new("IntValue",plr)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue",leaderstats)
cash.Name = "Cash"
cash.Value = 100
local key = ("Id-"plr..UserId)
if DS:GetAsync(key) then
cash.Value = Save[1]
else 
local Save = {cash.Value}
DS:SetAsync(key,Save)
end
end)

game.Players.PlayerRemoving:connect(function(plr)
local key = ("Id-"plr..UserId)
local Save = {plr.leaderstats.Cash.Value}
DS:SetAsync(key,Save)
end)

And nothing is saved after I left the game.

2 answers

Log in to vote
0
Answered by 5 years ago

first off, there are some mistakes in your current code:

local DS = (game:GetService("DataStoreService"):GetDataStore("sjisjsikjawju"))

game.Players.PlayerAdded:connect(function(plr)
    local leaderstats = Instance.new("IntValue",plr)
    leaderstats.Name = "leaderstats"
    local cash = Instance.new("IntValue",leaderstats)
    cash.Name = "Cash"
    cash.Value = 100
    local key = ("Id-" .. plr.UserId)            -- you used the .. on the wrong spot
    if DS:GetAsync(key) then
        cash.Value = Save[1]
    else
        local Save = {cash.Value}
        DS:SetAsync(key,Save)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = ("Id-" .. plr.UserId)           -- you used the .. on the wrong spot
    local Save = {plr.leaderstats.Cash.Value}
    DS:SetAsync(key,Save)
end)

second, you use cash.Value = Save[1] which is not the way a datastore's data is called, you want to use: local save = DS:GetAsync(key).

Ad
Log in to vote
0
Answered by
AIphanium 124
5 years ago
Edited 5 years ago

Try this Script; Based on my Comment and your Script.

Which is supposed to work :)

-- Note : DataStore won't work if its inside a LocalScript; Use a Script.
local DSS = game:GetService("DataStoreService")

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

local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue",leaderstats)
cash.Name = "Cash"
cash.Value = 100
end)

local datastore = DSS:GetDataStore("GeneralSaveData", "Players")

function generateDataKey(player)
    local ret = "uid_" .. player.userId
    return ret
end

function generateDataTable(player)
    local dataTable = {
        Cash = player.leaderstats.Cash.Value,

    }
    return dataTable
end

function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
    player.leaderstats.Cash.Value = data.Cash
end

function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:Connect(function(player)
    loadDataForPlayer(player) --Load first thing when they join
    player.leaderstats.Cash.Changed:connect(function()
        saveDataForPlayer(player) 
    end)
end)

game.Players.PlayerRemoving:Connect(saveDataForPlayer) --Save data when the player leaves the game

0
Nice stolen script User#19524 175 — 5y

Answer this question