Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

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

0

errors?

3 answers

✓ Accepted answer

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!

2 comments

0

Also, make sure you API Services ON in studio.

0

Thank you!

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)

1 comment

0

not save

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

1 comment

0

not save

This discussion is preserved as it was. New answers, comments, and votes are closed.