local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Epicness = Instance.new("IntValue")
Epicness.Name = "Epicness"
Epicness.Value = 0
Epicness.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
✓ ANSWER ACCEPTED
I need help i want this leaderstat value "epicness" to be saved can anyone show me how?
2 answers
✓ Accepted answer
If anyone viewing has a similar issue, we talked it over over discord and came up with this solution:
Comments are there explaining what each bit of code does, but comment if you need anymore explanation.
local DataStoreService = game:GetService("DataStoreService") --allows us to use datastores
local epicnessDatastore = DataStoreService:GetDataStore("EpicnessDatastore") --our datastore for saving the epicness value
--onPlayerJoin runs whenever the player joins the game.
function onPlayerJoin(player)
--creates an `epicness` intvalue for each player in the leaderboards.
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Epicness = Instance.new("IntValue")
Epicness.Name = "Epicness"
Epicness.Value = 0
Epicness.Parent = leaderstats
--get player's epicness value from the epicnessDatastore.
--what pcall does is stop the script from erroring,
--the epicness variable is the value that we got from the datastore,
--and success determines if the function ran without errors (true if no errors, false if it errors)
local success, epicness = pcall(function()
return epicnessDatastore:GetAsync(player.UserId) --get the data for the player
end)
--if the player has epicness value from the datastore, theyve played before, so change their epicness value on the leaderboard
--to be the value we got from datastore
if epicness then
player.leaderstats.Epicness.Value = epicness
end
end
function onPlayerRemoving(player)
print(player.Name .. " has left the game!")
--save the player's data when they leave the game
local success, err = pcall(function()
epicnessDatastore:SetAsync(player.UserId, player.leaderstats.Epicness.Value) --this line of code saves the player's epicness to the epicness datastore,
end)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
Assuming you already have a leaderstats, add something like this to it.
game.Players.PlayerAdded:Connect(function(plr)
local epicness = Instance.new("IntValue", folder)
epicness.Name = "Epicness"
epicness.Value = NameOfYourDataStore:GetAsync(plr.UserId)
NameOfYourDataStore:SetAsync(plr.UserId, epicness.Value)
epicness.Changed:connect(function()
NameOfYourDataStore:SetAsync(plr.UserId, epicness.Value)
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
