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

why this datastore script is not working to save everything on table?

Asked by
imKlevi 94
4 years ago

I want to save everything on table please help me this is not working!

local DataStoreService = game:GetService("DataStoreService")
local DataDS = DataStoreService:GetDataStore("OPMLData")

function getData(player)
  local success, dataFromDS = pcall(function()
        return DataDS:GetAsync(player.UserId)
    end)

  if dataFromDS then
    return dataFromDS
  else 
    return 0
  end

  if not success then
    warn("couldn't get the data properly")
  end
end

function saveData(player, valueToSave)
    local success, err = pcall(function()
            DataDS:SetAsync(player.UserId, valueToSave)
        end)

    if(not success)then
        warn("failed to save data")
    end
end

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

    local playerData = getData(player)

local keeper = Instance.new("Folder",game.ReplicatedStorage)
keeper.Name = player.Name.."Data"
local xp = Instance.new("NumberValue",keeper)
xp.Name = "XP"
xp.Value = 0
local xpneeded = Instance.new("NumberValue",keeper)
xpneeded.Name = "XPtonextlevel"
xpneeded.Value = 200
local lvl = Instance.new("NumberValue",keeper)
lvl.Name = "Level"
lvl.Value = 1
local beli = Instance.new("NumberValue",keeper)
beli.Name = "Beli"
local bounty = Instance.new("NumberValue",keeper)
bounty.Name = "Bounty"
end)
game.Players.PlayerRemoving:Connect(function(plr)
   print(plr.Name.." is leaving".. ", trying to save player's data!")
  local location = game.ReplicatedStorage[plr.Name.."Data"]
  local DataToSave = { location.Level.Value, location.XPtonextlevel.Value, location.Beli.Value, location.XP.Value, location.Bounty.Value }
for _,v in pairs(DataToSave) do
  saveData(plr, DataToSave)
end
   wait(0.1)
   game.ReplicatedStorage[plr.Name.."Data"]:Destroy()
end)

1 answer

Log in to vote
1
Answered by 4 years ago

Reanswering so I can post revised code for this. I removed the for loop, and made it so when you load the data, it can go into the correct values.

local DataStoreService = game:GetService("DataStoreService")
local DataDS = DataStoreService:GetDataStore("OPMLData")

function getData(player)
  local success, dataFromDS = pcall(function()
        return DataDS:GetAsync(player.UserId)
    end)

    if (success) then
        return dataFromDS
    else
        warn("couldn't get the data properly")
    end
end

function saveData(player, valueToSave)
    local success, err = pcall(function()
            DataDS:SetAsync(player.UserId, valueToSave)
        end)

    if(not success)then
        warn("failed to save data")
    end
end

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

local playerData = getData(player) or { 1, 200, 0, 0, 0 } --//Level; ExpNeeded; Beli; Exp; Bounty

local keeper = Instance.new("Folder",game.ReplicatedStorage)
keeper.Name = player.Name.."Data"
local xp = Instance.new("NumberValue",keeper)
xp.Name = "XP"
xp.Value = playerData[4]
local xpneeded = Instance.new("NumberValue",keeper)
xpneeded.Name = "XPtonextlevel"
xpneeded.Value = playerData[2]
local lvl = Instance.new("NumberValue",keeper)
lvl.Name = "Level"
lvl.Value = playerData[1]
local beli = Instance.new("NumberValue",keeper)
beli.Name = "Beli"
beli.Value = playerData[3]
local bounty = Instance.new("NumberValue",keeper)
bounty.Name = "Bounty"
bounty.Value = playerData[5]
end)
game.Players.PlayerRemoving:Connect(function(plr)
   print(plr.Name.." is leaving".. ", trying to save player's data!")
  local location = game.ReplicatedStorage[plr.Name.."Data"]
  local DataToSave = { location.Level.Value, location.XPtonextlevel.Value, location.Beli.Value, location.XP.Value, location.Bounty.Value }

  saveData(plr, DataToSave)
   wait(0.1)
   game.ReplicatedStorage[plr.Name.."Data"]:Destroy()
end)

This should work, tell me if there's any problems.

0
12:25:28.611 - ServerScriptService.AddLvandXP:34: attempt to index local 'playerData' (a number value) imKlevi 94 — 4y
0
12:25:30.667 - Beli is not a valid member of Folder imKlevi 94 — 4y
0
12:25:34.820 - Script 'ServerScriptService.AddLvandXP', Line 51 imKlevi 94 — 4y
Ad

Answer this question