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

my datastore not working, i don't know how to solve the problem?

Asked by 4 years ago
Edited 4 years ago

script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")

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

    local key = plr.UserId.."Key"

    local success, data = pcall(function()
        return DataStore:GetAsync(key)
    end)

    local folder = Instance.new("Folder", plr)
    folder.Name = "stats"

    local Coins = Instance.new("NumberValue", folder)
    Coins.Name = "Coins"
    Coins.Value = data[1]

    print(data[1])
    workspace.Coin.OnServerEvent:Connect(function(plr, add)
        Coins.Value = Coins.Value + 1
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local key = plr.UserId.."Key"

    local data = {
        plr.stats.Coins.Value

    }

    local success, err = pcall(function()
        DataStore:SetAsync(key, data)
    end)

end)

i think the problem is in the part when the player leaves the game, but I don't know how to solve it

0
Are you testing in studio if so thats why you have to test in the real game on roblox vortex767 20 — 4y
0
errors? Robowon1 323 — 4y

3 answers

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

Well, there are few flaws in your datastore code.

Flaws:

  1. You are assuming a string as a table, which is data.

  2. You are not assigning a variable for the returning function in pcall().

  3. There is no BindToClose function.

  4. You are not checking whether the data exists or not (for new players).

Possible solutions :

  1. Do what, @TheMinecraftSmarts have said :
local data
local success, errormessage = pcall(function()
    data = DataStore:GetAsync(key)
end)
  1. Check whether the data exists :
if success and data then -- It will ensure whether the data is not nil and exists
  1. Make a default value :
-- Instead of 
Coins.Value = data[1]

-- Make it
Coins.Value = 0 -- or whatever value you want to have for new players
  1. If the data exists, give the coins saved in the data :
if success and data then
    Coins.Value = data[1]
else
    warn(errormessage)
end
  1. Add a BindToClose function :
local RunService = game:GetService("RunService")

game:BindToClose(function()
    if RunService:IsStudio() then return end
    for _, plr in ipairs(game.Players:GetPlayers()) do
        -- Your code for saving data
    end
end)

What's the full output code after edits?

  1. For testing in Studio, it should be like :
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")

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

    local key = plr.UserId.."Key"

    local data
    local success, errormessage = pcall(function()
        data = DataStore:GetAsync(key)
    end)

    local folder = Instance.new("Folder", plr)
    folder.Name = "stats"

    local Coins = Instance.new("NumberValue", folder)
    Coins.Name = "Coins"
    Coins.Value = 0 -- As example

    print(data[1])

    if success and data then
        Coins.Value = data[1]
    else
        warn(errormessage)
    end

    workspace.Coin.OnServerEvent:Connect(function(plr, add)
        Coins.Value = Coins.Value + 1
    end)
end)

--[[ game.Players.PlayerRemoving:Connect(function(plr)
    local key = plr.UserId.."Key"

    local data = {
        plr.stats.Coins.Value

    }

    local success, err = pcall(function()
        DataStore:SetAsync(key, data)
    end)

end) ]]

game:BindToClose(function()
--  if RunService:IsStudio() then return end
    for _, plr in ipairs(game.Players:GetPlayers()) do
        local key = plr.UserId.."Key"

            local data = {
                plr.stats.Coins.Value

         }

            local success, err = pcall(function()
                DataStore:SetAsync(key, data)
        end)
    end
end)
  1. For Roblox Real server (during publish) :
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")

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

    local key = plr.UserId.."Key"

    local data
    local success, errormessage = pcall(function()
        data = DataStore:GetAsync(key)
    end)

    local folder = Instance.new("Folder", plr)
    folder.Name = "stats"

    local Coins = Instance.new("NumberValue", folder)
    Coins.Name = "Coins"
    Coins.Value = 0 -- As example

    print(data[1])

    if success and data then
        Coins.Value = data[1]
    else
        warn(errormessage)
    end

    workspace.Coin.OnServerEvent:Connect(function(plr, add)
        Coins.Value = Coins.Value + 1
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local key = plr.UserId.."Key"

    local data = {
        plr.stats.Coins.Value

    }

    local success, err = pcall(function()
        DataStore:SetAsync(key, data)
    end)

end) 

game:BindToClose(function()
    if RunService:IsStudio() then return end
    for _, plr in ipairs(game.Players:GetPlayers()) do
        local key = plr.UserId.."Key"

            local data = {
                plr.stats.Coins.Value

         }

            local success, err = pcall(function()
                DataStore:SetAsync(key, data)
        end)
    end
end)

Lemme know if it helps!

0
Also, make sure you API Services ON in studio. BestCreativeBoy 1395 — 4y
0
Thank you! zer_00Y 2 — 4y
Ad
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago

I think the second parameter of a pcall is reserved for error message codes. Why don't you try this:

local data

local success, errormessage = pcall(function()
    data = DataStore:GetAsync(key)
end)
0
not save zer_00Y 2 — 4y
Log in to vote
0
Answered by 4 years ago

Two things -

1) Are you testing in studio if so thats why you have to test in the real game on roblox

2) you should have it do this

  local folder = Instance.new("Folder", plr)
    folder.Name = "stats"

    local Coins = Instance.new("NumberValue", folder)
    Coins.Name = "Coins"

local success, data = pcall(function()
        return DataStore:GetAsync(key)
    end)

if success then
    Coins.Value = data[1]
end
0
not save zer_00Y 2 — 4y

Answer this question