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

(?) I need help converting this script into leaderstats saving script

Asked by 6 years ago
Edited 6 years ago

my problem is that every save script i have is broken and i need an script that will save like this one

local ds = game:GetService("DataStoreService"):GetDataStore("-tCodes01")

game.Players.PlayerAdded:Connect(function(player)
    local key = "codes-"..player.userId
    local folder = Instance.new("Folder",player)
    folder.Name = "Codes"
    local save = ds:GetAsync(key)
    if save then
        for i = 1,#save do
            local temp = Instance.new("BoolValue",folder)
            temp.Name = save[i]
        end
    end
end)

game.ReplicatedStorage.EnterCode.OnServerEvent:Connect(function(player,reward,code)
    local key = "codes-"..player.userId
    local currency = player.leaderstats:FindFirstChild("Money") -- Name of your currency
    currency.Value = currency.Value + reward

    local bool = Instance.new("BoolValue",player.Codes)
    bool.Name = code
    bool.Value = true

    local activated = {}
    for i,v in pairs(player.Codes:GetChildren()) do
        if v:isA("BoolValue") then
            table.insert(activated, v.Name)
            ds:SetAsync(key,activated)
        end
    end
end)
0
You haven't explained the problem you have been having, without this we cannot help you. awfulszn 394 — 6y
0
i need help to make this script save leaderstats FluxRBLX -5 — 6y
0
and not save codes FluxRBLX -5 — 6y
0
i need it to save money grass Total grass and Rebirths FluxRBLX -5 — 6y
View all comments (4 more)
0
let me guess you took this script from somewhere else GamingOverlord756 48 — 6y
0
i just need help FluxRBLX -5 — 6y
0
XxxxLloydxxX lol User#19524 175 — 6y
0
This website is for asking problems with your scripting, not a free scripting service when something for you doesn't work or you want something to change. Warfaresh0t 414 — 6y

1 answer

Log in to vote
0
Answered by 4 years ago

I have a script for leaderstats but if you ever want to reset the leaderstats, it won't work since the data is burned onto the game. Here is the script:

local dataStoreService = game:GetService("DataStoreService") local myDataStore = dataStoreService:GetDataStore("myDataStore") local players = game:GetService("Players") local plrsLeft = 0

players.PlayerAdded:Connect(function(player) plrsLeft = plrsLeft + 1 local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Coins"  

--Change this name to the name of your currency. cash.Parent = leaderstats

local success1,response1 = pcall(myDataStore.GetAsync,myDataStore,player.UserId)
if success1 then
    cash.Value = response1
else
    warn(response1)
end

while true do
    wait(30)
    local success2,response2 = pcall(myDataStore.UpdateAsync,myDataStore,player.UserId,function(oldValue)
        return cash.Value
    end)
    if not success2 then
        warn(response2)
    end
end

end)

local BindEvent = Instance.new("BindableEvent") players.PlayerRemoving:Connect(function(player) plrsLeft = plrsLeft - 1

local success,response = pcall(myDataStore.UpdateAsync,myDataStore,player.UserId,function(oldValue)
    return player.leaderstats.Coins.Value
end)
if success then
    BindEvent:Fire()
else
    warn(response)
end

end)

game:BindToClose(function() while plrsLeft > 0 do BindEvent.Event:Wait() end end)

Ad

Answer this question