Hello so my data store isnt working.Ive tried moving it to ServerScriptService it doesnt save or load anything.There are no output by this script at all.And yes API services are enabled.
local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
--level
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
Level.Value = 1
--tokens
local PlayerValueFolder = Instance.new("Folder")
PlayerValueFolder.Parent = player
PlayerValueFolder.Name = "PlayerValueFolder"
local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Parent = PlayerValueFolder
--Exp
local Exp = Instance.new("IntValue")
Exp.Name = "Exp"
Exp.Parent = PlayerValueFolder
Exp.Value = 0
--ExpNeeded
local ExpNeeded = Instance.new("IntValue")
ExpNeeded.Name = "ExpNeeded"
ExpNeeded.Parent = PlayerValueFolder
while true do
wait(1)
ExpNeeded.Value = Level.Value*20
end
local LevelData
local TokensData
local ExpData
local Success, ErrorMessage = pcall(function()
LevelData = DataStore:GetAsync(player.UserId.."-LevelData")
TokensData = DataStore:GetAsync(player.UserId.."-TokensData")
ExpData = DataStore:GetAsync(player.UserId.."-ExpData")
end)
if not Success then
print(ErrorMessage)
end
if LevelData ~= nil then
Level.Value = LevelData
else
Level.Value = 1
end
if TokensData ~= nil then
Tokens.Value = TokensData
else
Tokens.Value = 0
end
if ExpData ~= nil then
Exp.Value = ExpData
else
Exp.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
pcall(function()
DataStore:SetAsync(Player.UserId.."-LevelData", Player.leaderstats.Level.Value)
DataStore:SetAsync(Player.UserId.."-TokensData", Player.PlayerValueFolder.Tokens.Value)
DataStore:SetAsync(Player.UserId.."-ExpData", Player.PlayerValueFolder.Exp.Value)
end)
end)
The problem is that Studio closes the game and doesnt run the PlayerRemoving function. Test this in game for it to work or do it in a game:BindToClose function.
Ok, the problem is that the BindToClose function does not give a player variable. You would have to get the player like this:
game:BindToClose(function()
for i, Player in pairs(game.Players:GetPlayers()) do
pcall(function()
DataStore:SetAsync(Player.UserId.."-LevelData", Player.leaderstats.Level.Value)
DataStore:SetAsync(Player.UserId.."-TokensData", Player.PlayerValueFolder.Tokens.Value)
DataStore:SetAsync(Player.UserId.."-ExpData", Player.PlayerValueFolder.Exp.Value)
end)
end
end)
Update:It still doesnt work sadly.No output error or anything like that.
Updated code:
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“DataStore”)
game.Players.PlayerAdded:Connect(function(player)
–level
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Level = Instance.new(“IntValue”)
Level.Name = “Level”
Level.Parent = leaderstats
Level.Value = 1
–tokens
local PlayerValueFolder = Instance.new(“Folder”)
PlayerValueFolder.Parent = player
PlayerValueFolder.Name = “PlayerValueFolder”
local Tokens = Instance.new(“IntValue”)
Tokens.Name = “Tokens”
Tokens.Parent = PlayerValueFolder
–Exp
local Exp = Instance.new(“IntValue”)
Exp.Name = “Exp”
Exp.Parent = PlayerValueFolder
Exp.Value = 0
–ExpNeeded
local ExpNeeded = Instance.new(“IntValue”)
ExpNeeded.Name = “ExpNeeded”
ExpNeeded.Parent = PlayerValueFolder
while true do
wait(1)
ExpNeeded.Value = Level.Value*20
end
local LevelData
local TokensData
local ExpData
local Success, ErrorMessage = pcall(function()
LevelData = DataStore:GetAsync(player.UserId…"-LevelData")
TokensData = DataStore:GetAsync(player.UserId…"-TokensData")
ExpData = DataStore:GetAsync(player.UserId…"-ExpData")
end)
if not Success then
print(ErrorMessage)
end
if LevelData ~= nil then
Level.Value = LevelData
else
Level.Value = 1
end
if TokensData ~= nil then
Tokens.Value = TokensData
else
Tokens.Value = 0
end
if ExpData ~= nil then
Exp.Value = ExpData
else
Exp.Value = 0
end
end)
game:BindToClose(function()
for i, Player in pairs(game.Players:GetPlayers()) do
pcall(function()
DataStore:SetAsync(Player.UserId…"-LevelData", Player.leaderstats.Level.Value)
DataStore:SetAsync(Player.UserId…"-TokensData", Player.PlayerValueFolder.Tokens.Value)
DataStore:SetAsync(Player.UserId…"-ExpData", Player.PlayerValueFolder.Exp.Value)
end)
end
end)
I’m not sure the exact problem then, but I would still recommend keeping the PlayerRemoving function for it to save when someone leaves and the game doesn’t shutdown.