local GameService = game:GetService("DataStoreService")
local MyDataStore = GameService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(players)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = players
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats local data local success, errormessage = pcall(function() data = MyService:GetAsync(players.UserId)
end)
if success then Points.Value = data else
warn(errormessage)
end end) game.Players.PlayerRemoving:Connect(function(players)
local success, errormessage = pcall(function()
local data = MyService:SetAsync(players.UserId, players.leaderstats.Points.Value)
end)
if success then
print("Saved Successfuly")
else print("Saved failed") warn(errormessage)
end end)
game:BindToClose(function()
for i, players in pairs(game.Players:GetChildren()) do
players:Kick("Server Closed") end end)
Here, try my(Not actually mine got from yt vid) datastore script. You can change you leaderstats to yours. I have 2 leaderstats, though thant might not make a difference. Also, make sure HTTP requests are enabled under security:
local dataStoreService = game:GetService("DataStoreService") local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats") local loaded = {} game.Players.PlayerAdded:Connect(function(player) local leaderstats = player:WaitForChild("leaderstats") if player.UserId > 0 and player.Parent then local leaderstatsdata = leaderstatsDataStore:GetAsync(player.UserId) if leaderstatsdata ~= "Request Rejected" then if leaderstatsdata then for i, stat in ipairs(leaderstats:GetChildren()) do local value = leaderstatsdata[stat.Name] if value then stat.Value = value end end end loaded[player] = true end end end) game.Players.PlayerRemoving:Connect(function(player) local leaderstats = player:FindFirstChild("leaderstats") if leaderstats then if loaded[player] then local leaderstatsData = {} for i, stat in ipairs(leaderstats:GetChildren()) do leaderstatsData[stat.Name] = stat.Value end leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData) end end loaded[player] = nil end)
Hope his works!!!