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

How to prevent my datastore from giving nil instead it should save data?

Asked by 6 years ago

I have a datastore script that saves a players money for the next time they join their money is stored in ServerStorage.PlayerMoney.Playername This is the script i currently have

The script is in ServerScriptService and is a server side script.

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
   -- Define variables
   print('waiting until tycoonscript ready')
   wait(6)
   print('Starting')
   local uniquekey = 'id-'..plr.userId
   local Moneystorage = game.ServerStorage:FindFirstChild('PlayerMoney')
   local PlayerMoney = Moneystorage:FindFirstChild(plr.Name)
   local savevalue =  PlayerMoney.Value
    --leaderstats.Name = 'leaderstats'
    --savevalue.Parent = leaderstats
    --savevalue.Name = 'Cash'

    -- GetAsync
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
    print('Loading data')
    print(GetSaved)
    print(GetSaved.Value)
       savevalue = GetSaved[1]
   else
       local NumbersForSaving = {savevalue.Value}
pcall(function()
       DSService:SetAsync(uniquekey, NumbersForSaving)
end)
        end
    end)

game.Players.PlayerRemoving:connect(function(plr)
local Moneystorage = game.ServerStorage:FindFirstChild('PlayerMoney')
local PlayerMoney = Moneystorage:FindFirstChild(plr.Name)
local savevalue =  PlayerMoney
local uniquekey = 'id-'..plr.userId
local Savetable = {savevalue.Value}
pcall(function()
DSService:SetAsync(uniquekey, Savetable)  
end)     
end)

This is the output in game. https://imgur.com/a/sPeBJ I set the money of my alt to 1000 then i let it rejoin but it still shows nil. How to make it output the money thats saved and give it to the player

0
GetSaved is the table, so GetSaved[1] would be the value. UgOsMiLy 1074 — 6y
0
Thanks for this i managed to fix it now. I didn't understand where the value would be I thought it was in GetSaved.Value Timmerman73 85 — 6y

Answer this question