So im having trouble with saving players leaderstats and im kind of new to datastores and note I was watching thedevkings video so idk what I did wrong.
local plr = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local MyData = DSS:GetDataStore("MyData1") -- gets a data store and you can name it
--- plr name
plr.PlayerAdded:Connect(function(player)
print(player.Name)
local text = game.StarterGui.ScreenGui.TextLabel
text.Text = player.Name
--- leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 100
while true do
task.wait(5)
Coins.Value = Coins.Value + 1
end
-- datastore
local PlrId = "Player_"..player.UserId
-- syncs player data from datastore GetAsync()
local data1
local success, errormessage = pcall(function() -- pcall stops errors
data1 = MyData:GetAsync(PlrId)
end)
if success then
-- set our data to our coin value
Coins.Value = data1
end
plr.PlayerRemoving:Connect(function(player)
local PlrId = "Player_"..player.UserId
local plrdata = plr:WaitForChild(leaderstats)
local success, errormessage = pcall(function()
MyData:SetAsync(PlrId, plrdata)
end)
if success then
print("SUCCESS")
else
print("not good")
warn(errormessage)
end
end)
end)

1 comment
Just wanted to add that the
while true doloop in your script completely stops the rest of the script from executing.