What's wrong with this data store?

My data store isn’t saving data when a player leaves and rejoins the game.


local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.Userid.."-wins")
	end)
	if success then
		wins.Value = data
	else
		print("There was an erro whilst getting your data")
		warn(errormessage)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
	end)
	if success then
		print("Player Data Successfully Saved")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)
1 Like

idk why it wont work but those types of scripts dont usually work so here’s one that’ll work:
PUT THIS AS A SCRIPT IN WORKSPACE


local ds = DataStore:GetDataStore("WinsSaveSystem")

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

local leader = Instance.new("Folder",player)

leader.Name = "leaderstats"

local wins = Instance.new("IntValue",leader)

wins.Name = "Wins"

wins.Value = ds:GetAsync(player.UserId) or 0

ds:SetAsync(player.UserId, wins.Value)

wins.Changed:connect(function()

ds:SetAsync(player.UserId, wins.Value)

end)

end)

game.Players.PlayerRemoving:connect(function(player)

ds:SetAsync(player.UserId, player.leaderstats.wins.Value)

end)```
1 Like

were there any errors in the script?

if not try using BindToClose (DataModel | Documentation - Roblox Creator Hub)

1 Like

There doesn’t seem to be anything wrong with the script, so it is likely a problem with something else. Is all of this code running? Did you check the console to see if it saved correctly?

If your only testing this in studio, it usually wont work because the server is instantly shutdown and there isn’t enough time to finish saving data. To fix this you can add a wait(2) in the game.BindToClose event so it has two seconds to save.

1 Like

@YummyLava57 This didn’t work, at least not how I implemented it. Is there anything wrong with how I implemented it?


local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local Players = game:GetService("Players")

game:BindToClose(function()
	wait(2)
end)

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.Userid.."-wins")
	end)
	if success then
		wins.Value = data
	else
		print("There was an erro whilst getting your data")
		warn(errormessage)
	end
end)



Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
	end)
	if success then
		print("Player Data Successfully Saved")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)
1 Like

So I tried @iclimbedwall’s solution and it didn’t work either, I’ll send the log as well as the script.

Log:

Script:


local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("WinsSaveSystem")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = ds:GetAsync(player.UserId) or 0
	wins.Parent = leaderstats
	
	ds:SetAsync(player.UserId, wins.Value)
	wins.Changed:connect(function()
		ds:SetAsync(player.UserId, wins.Value)
	end)
	
end)

Players.PlayerRemoving:Connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.wins.Value)
end)

1 Like

After looking at the logs I noticed that you forgot to capitalize the I in player.UserId inside GetAsync.

data = myDataStore:GetAsync(player.Userid…“-wins”)


This is what it should look like

data = myDataStore:GetAsync(player.UserId…“-wins”)

Everything else looks right.

2 Likes

Okay so it didn’t work because you need to have 2 separate scripts for each datastore, you can’t put them all in one. Also if you haven’t already make sure they’re in workspace and lmk if it works