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

Script To Save data leaderstats and where to make script?

Asked by 3 years ago
Edited 3 years ago

i need to know how to save for my simulator so yeah im kinda dumb as crap :D so yeah also if u can help me with developer products that increase leader stats that would help 2 :D my leaderstat is called "Muscle"

3 answers

Log in to vote
0
Answered by 3 years ago

I seriously need help

Ad
Log in to vote
0
Answered by
DemGame 271 Moderation Voter
3 years ago

Here, I wrote a script for you:

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
    local MuscleValue = player:WaitForChild("leaderstats"):WaitForChild("Muscle")

    local Data

    local Success, ErrorMessage = pcall(function()
        Data = DataStore:GetAsync(player.UserId.."-Muscle")
    end)

    if Success then
        MuscleValue.Value = tonumber(Data)
    else
        print("There was an error loading "..player.Name.."'s data.")
        warn(ErrorMessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Success, ErrorMessage = pcall(function()
        DataStore:SetAsync(player.UserId.."-Muscle", player:FindFirstChild("leaderstats"):FindFirstChild("Muscle"))
    end)

    if Success then
        print(player.Name.."'s Data was successfully saved.")
    else
        print(player.Name.."'s Data failed to save.")
        warn(ErrorMessage)
    end
end)

An in-depth explanation would be very long and impractical to put here, so here's a link to one if you would like to know how this script works.

https://www.youtube.com/watch?v=DkYupSBUpes

0
To use this script, place I would recommend placing it inside of ServerScriptService. DemGame 271 — 3y
0
didnt work' spideyweartideys 7 — 3y
0
my tool isnt owrkin now.... spideyweartideys 7 — 3y
Log in to vote
0
Answered by 3 years ago

Use data store, use a normal script, and put it in serverscriptservice or workspace https://developer.roblox.com/en-us/articles/Data-store

Answer this question