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

How to fix bug with leaderstats data saving?

Asked by
kapipi12 133
6 years ago

I need to save leaderstats in DataStore. My leaderstats are:

Cash

InBucket

Catches

I need a universal but simple script for saving these, and also other values I didn't mention. I know there ARE TONS of tutorials and guides for it but seems like none helped me yet (out of date or too complex). Also, I already have these values created in a script, and I want to save in another, so please do not make them again. (IK it isn't a request site, if you are against requesters, try to at least explain.) Please help... Any help is much appreciated. Also, I deleted the script cause it didn't work, if you could explain me how to create new simple one, i would really appreciate it :)

1 answer

Log in to vote
2
Answered by
Kikitob 105
6 years ago

Ok I wrote this script for you that should save the stats. If you have any errors dm me and I will try to fix them. You could also look at the wiki for later stuff, for example DataStore in this case https://developer.roblox.com/api-reference/function/DataStoreService/GetDataStore .

local DataStore = game:GetService("DataStoreService")
local Http = game:GetService("HttpService")
local Stats = DataStore:GetDataStore("Stats")

function GetStats(Player)
    local Success,Return = pcall(function()
        return Stats:GetAsync(Player.UserId)
    end)
    if Success then
        Return = Http:JSONDecode(Return)
        return Return
    else
        print("Getting data error :"..Return)
        return {}
    end
end

function SetStats(Player)
    local info = {}
    info.Cash = Player.Leaderstats.Cash.Value
    info.InBucket = Player.Leaderstats.InBucket.Value
    info.Catches = Player.Leaderstats.Catches.Value
    info = Http:JSONEncode(info)
    local Success,Error = pcall(function()
        Stats:SetAsync(Player.UserId,info)
    end)
    if not Success then
        print("Setting data error :"..Error)
    end
end

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    local Stats = GetStats(Player)
    Player:WaitForChild("Leaderstats"):WaitForChild("Cash").Value = Stats.Cash
    Player:WaitForChild("Leaderstats"):WaitForChild("InBucket").Value = Stats.InBucket
    Player:WaitForChild("Leaderstats"):WaitForChild("Catches").Value = Stats.Catches
end)

Players.PlayerRemoving:Connect(SetStats)
Ad

Answer this question