I’m trying to make a script that saves the leader Stats to a datastore. Yet it won’t output anything, and it won’t save either.
I have looked for solutions and I HAVE API ON.
Also here is the code.
Script 1, AKA “Leaderstats” As seen in the video.
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Coins = Instance.new("IntValue", leaderstats)
Coins.Value = 0
Coins.Name = "Coins"
end)
Script 2, AKA “Script” As seen in the video.
local DataStoreService = game:GetService("DataStoreService")
local leaderstatsData = DataStoreService:GetDataStore("LeaderstatsData")
game.Players.PlayerAdded:Connect(function(player)
local playerData = leaderstatsData:GetAsync(player.UserId)
if not playerData then
playerData = {
Coins = 0
}
leaderstatsData:SetAsync(player.UserId, playerData)
end
player.leaderstats.Coins.Value = playerData.Coins
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerData = {
Coins = player.leaderstats.Coins.Value
}
leaderstatsData:SetAsync(player.UserId, playerData)
end)
Please comment if you have any possible fixes.