Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need help i want this leaderstat value "epicness" to be saved can anyone show me how?

Asked by 4 years ago
Edited by royaltoe 4 years ago
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)



2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

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)
Ad
Log in to vote
1
Answered by 4 years ago

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)

Answer this question