I tried a lot of resources, but I am unable to make the gold, tool hardness, tool power, or spaces save. I would like it to save when a player leaves, and load when a player joins. I tried to get it to save and load, but It still doesn't work, nor show any sort of error, so i don't know what's wrong. It would be a huge help if this could save/load. also, everything below this is my code. The script is in the ServerScriptService.
-- Creates a leaderboard that shows player variables
local Players = game:GetService("Players")
local goldDataStore = game:GetService("DataStoreService"):GetDataStore("Data0")
local function onPlayerJoin(player)
local playerKey = "Player_" .. player.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue", leaderstats)
gold.Name = "Gold"
local startgold = 0
game.Players.PlayerAdded:Connect(function()
local myGold= goldDataStore:GetAsync(playerKey,{player.leaderstats.Gold.Value
})
gold.Value = myGold
end)
local Items= Instance.new("IntValue")
Items.Name = "Items"
Items.Value = 0
Items.Parent = leaderstats
local Spaces = Instance.new("IntValue")
Spaces.Name = "Spaces"
Spaces.Value = 2
Spaces.Parent = leaderstats
local Values = Instance.new("Folder")
Values.Name = "Values"
Values.Parent = player
local Toolhardness = Instance.new("IntValue")
Toolhardness.Name = "ToolHardness"
Toolhardness.Value = 1
Toolhardness.Parent = Values
local ToolPower = Instance.new("IntValue")
ToolPower.Name = "ToolPower"
ToolPower.Value = 1
ToolPower.Parent = Values
end
-- Run onPlayerJoin when the PlayerAdded event fires
for _, player in pairs(Players:GetPlayers()) do
onPlayerJoin(player)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
local Players = game:GetService("Players")
local goldDataStore = game:GetService("DataStoreService"):GetDataStore("Data0")
local function Saveonleave(player)
local playerKey = "Player_" .. player.UserId
goldDataStore:SetAsync(playerKey,{
player.leaderstats.Gold.Value
})
end
game.Players.PlayerRemoving:Connect(Saveonleave)

1 comment
Thanks. I was just having a rough time getting the save function to work, and, again, thanks.