Datastore script is not working

Hi, I have a datastore script that saves both the coins that a player has, and their wins. It correctly saves their coins, however every time you join the game, the wins reset. Here is my script:

local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local WinStore = DataStoreService:GetDataStore("WinStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    Player.TeamColor = BrickColor.new("Really red")
    local UserId = Player.UserId
    local Currency = CurrencyStore:GetAsync(UserId)
    

    if Currency == nil then
        Currency = 0
        CurrencyStore:SetAsync(UserId, Currency)
    end

    local Wins = WinStore:GetAsync(UserId)
    if Wins == nil then
        Wins = 0
        WinStore:SetAsync(UserId, Wins)
    end

    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "leaderstats"

    local Coins = Instance.new("IntValue", Leaderstats)
    Coins.Name = "Coins"

    Coins.Value = Currency
    local Wins = Instance.new("IntValue", Leaderstats)
    Wins.Name = "Wins"

    Wins.Value = Wins
    
    
    

    Coins.Changed:Connect(function(NewValue)
        CurrencyStore:SetAsync(UserId, NewValue)
    end)
    Wins.Changed:Connect(function(NewValue)
        WinStore:SetAsync(UserId, NewValue)
    end)
end)

Why is this?

I recommend using pcall for Datastore-related stuff.
Here’s a good thread about pcall: Pcalls - When and how to use them

local Success, Error = pcall(function()

end)

if Success then
Coins/Wins.Value = value saved by datastore
end

Hi, instead of using SetAsync() every time the coins and wins change, you can save the data when the player leaves. You can also save the data in the same datastore but using a table. Also use DataModel | Roblox Creator Documentation to save the data before the game shutdown and be sure to test it on the roblox game client, not in roblox studio.

wait, so it auto saves it whenever a player leaves? -meaning I don’t have to manually save it?-

I will remember that, however that’s not the reason that it is not working…

You can save a player’s data with the PlayerRemoving event.

The coins datastore is working, and I tried to make the coins data store identical to the wins data store, but it’s not working.

Hi, why do you use SetAsync() right when the player joins? Instead of doing that, you can check if the data exists.

Check it this works

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

Players.PlayerAdded:Connect(function(Player)
	Player.TeamColor = BrickColor.new("Really red")
	local UserId = Player.UserId
	local data
	
	local success, err = pcall(function()
		data = PlayerDataStore:GetAsync(UserId)
	end)

	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Coins = Instance.new("IntValue", Leaderstats)
	Coins.Name = "Coins"

	local Wins = Instance.new("IntValue", Leaderstats)
	Wins.Name = "Wins"
	
	if success and data then
		Coins.Value = data.Coins
		Wins.Value = data.Wins
	elseif not success then
		warn(err)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local data = {
		Coins = Player.leaderstats.Coins.Value,
		Wins = Player.leaderstats.Wins.Value
	}
	local success, err = pcall(function()
		PlayerDataStore:SetAsync(Player.UserId, data)
	end)
	if not success then
		warn(err)
	end
end)

game:BindToClose(function()
	if RunService:IsStudio() then
		return
	end
	for _, player in pairs(Players:GetPlayers()) do
		local data = {
			Coins = player.leaderstats.Coins.Value,
			Wins = player.leaderstats.Wins.Value
		}
			local success, err = pcall(function()
			PlayerDataStore:SetAsync(player.UserId, data)
		end)
		if not success then
			warn(err)
		end
	end
end)

Btw is there an error in the output in your script?

Sorry that I didn’t respond until now- both my script and your script both don’t work, but I don’t have any error :confused:

Are you testing it on the roblox game client?

That is false. You need to set up a function to save data on player leaving.

I’m testing it on roblox studio so I can check if there are errors as well as in the actual game

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

Players.PlayerAdded:Connect(function(Player)
	Player.TeamColor = BrickColor.new("Really red")
	local UserId = Player.UserId
	local data
	
	local success, err = pcall(function()
		data = PlayerDataStore:GetAsync(UserId)
	end)

	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Coins = Instance.new("IntValue", Leaderstats)
	Coins.Name = "Coins"

	local Wins = Instance.new("IntValue", Leaderstats)
	Wins.Name = "Wins"
	
	if success and data then
		Coins.Value = data.Coins
		Wins.Value = data.Wins
	elseif not success then
		warn(err)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local data = {
		Coins = Player.leaderstats.Coins.Value,
		Wins = Player.leaderstats.Wins.Value
	}
	local success, err = pcall(function()
		PlayerDataStore:SetAsync(Player.UserId, data)
	end)
	if not success then
		warn(err)
	end
end)

game:BindToClose(function()
	if RunService:IsStudio() then
		wait(1)
	end
	for _, player in pairs(Players:GetPlayers()) do
		local data = {
			Coins = player.leaderstats.Coins.Value,
			Wins = player.leaderstats.Wins.Value
		}
			local success, err = pcall(function()
			PlayerDataStore:SetAsync(player.UserId, data)
		end)
		if not success then
			warn(err)
		end
	end
end)

Try this

Can you show that code, so we can try to find the error? (the script that changes values ​​in leaderstats)

My original script? It is this:

local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local WinStore = DataStoreService:GetDataStore("WinStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.TeamColor = BrickColor.new("Really red")
	local UserId = Player.UserId
	local Currency = CurrencyStore:GetAsync(UserId)
	

	if Currency == nil then
		Currency = 0
		CurrencyStore:SetAsync(UserId, Currency)
	end

	local Wins = WinStore:GetAsync(UserId)
	if Wins == nil then
		Wins = 0
		WinStore:SetAsync(UserId, Wins)
	end

	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Coins = Instance.new("IntValue", Leaderstats)
	Coins.Name = "Coins"

	Coins.Value = Currency
	local Wins = Instance.new("IntValue", Leaderstats)
	Wins.Name = "Wins"

	Wins.Value = Wins
	
	
	

	Coins.Changed:Connect(function(NewValue)
		CurrencyStore:SetAsync(UserId, NewValue)
	end)
	Wins.Changed:Connect(function(NewValue)
		WinStore:SetAsync(UserId, NewValue)
	end)
end)

while true do
	wait(0.1)
	for _, plr in pairs(game.Players:GetChildren()) do

	
		if plr.TeamColor == BrickColor.new("Lime green") then
			
			print(#game.Teams.Alive:GetPlayers())
			if #game.Teams.Alive:GetPlayers() < 1.5 then

				plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
				plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 100
			
				wait(1.6)
			end	



		end

	end

end

(the other part of the script gives the pllayer coins and a win if they are the last one standing)

Did you try my script?

{Ignore this}

I just checked it, and it worked :smiley: thanks

1 Like