I literally have no idea why the following code isn’t working, it does not give any messages nor errors in output. I’m trying to save the player’s coins.
local players = game.Players
local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetDataStore("PlayerData")
local success,data pcall(function()
playerdata:GetAsync(player.UserId)
end)
if not success then
wait(.5)
GetPlayerData(player)
else
if data then
player:WaitForChild("leaderstats").Coins.Value = data
else print(player.Name .. "has no data")
end
end
print("Player has " .. player:WaitForChild("leaderstats").Coins.Value)
end
function onPlayerEntered(player) --function for creating data when a player joins
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 100
money.Parent = stats
GetPlayerData(player)
end
players.ChildAdded:Connect(onPlayerEntered)
function SavePlayerData(player) --saving player's data when they leave
local playerdata = datastoreservice:GetDataStore("PlayerData")
local success = pcall(function()
playerdata:SetAsync(player.UserId, player:WaitForChild("leaderstats").Coins.Value)
end)
if success then
player:WaitForChild("leaderstats").Coins:Destroy()
print("sucessfully saved " .. player.Name .. "'s data")
else
wait(.5)
SavePlayerData(player)
end
end
players.PlayerRemoving:Connect(function(player)
print(player.Name .. " has left the server, saving data")
SavePlayerData(player)
end) ```
I have looked at the developer console in game + used crazyman32's datastore editor to view the datastore. No data was found.