Ad
Log in to vote
0

How do I reset my Datastorage for everyone in this script?

Asked by
Cowgato 33
18 days ago

for more context, the name of the Modul Script is PlayerManager

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function LeaderboardSetup(value)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = value
    money.Parent = leaderstats
    return leaderstats
end

local function LoadData(player)
    local success, result = pcall(function()
        return PlayerData:GetAsync(player.UserId)
    end)
    if not success then
        warn(result)
    end
    return success, result
end

local function SaveData(player, data) -- change data
    local success, result = pcall(function()
        PlayerData:SetAsync(player.UserId, data) -- change data
    end)
    if not success then
        warn(result)
    end
    return success
end

local sessionData = {}

local playerAdded = Instance.new("BindableEvent")
local playerRemoving = Instance.new("BindableEvent")

local PlayerManager = {}

PlayerManager.PlayerAdded = playerAdded.Event
PlayerManager.PlayerRemoving = playerRemoving.Event

function PlayerManager.Start()
    for _, player in ipairs(Players:GetPlayers()) do
        coroutine.wrap(PlayerManager.OnPlayerAdded)(player)
    end

    Players.PlayerAdded:Connect(PlayerManager.OnPlayerAdded)
    Players.PlayerRemoving:Connect(PlayerManager.OnPlayerRemoving)

    game:BindToClose(PlayerManager.OnClose)
end

function PlayerManager.OnPlayerAdded(player)
    player.CharacterAdded:Connect(function(character)
        PlayerManager.OnCharacterAdded(player, character)
    end)

    local success, data = LoadData(player)
    sessionData[player.UserId] = success and data or {
        Money = 0,
        UnlockIds = {}
    }

    local leaderstats = LeaderboardSetup(PlayerManager.GetMoney(player))
    leaderstats.Parent = player

    playerAdded:Fire(player)
end

function PlayerManager.OnCharacterAdded(player, character)
    local humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Died:Connect(function()
            wait(3)
            player:LoadCharacter()
        end)
    end
end

function PlayerManager.GetMoney(player)
    return sessionData[player.UserId].Money
end

function PlayerManager.SetMoney(player, value)
    if value then
        sessionData[player.UserId].Money = value

        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            local money = leaderstats:FindFirstChild("Money")
            if money then
                money.Value = value
            end
        end     
    end
end

function PlayerManager.AddUnlockId(player, id)
    local data = sessionData[player.UserId]

    if not table.find(data.UnlockIds, id) then
        table.insert(data.UnlockIds, id)
    end
end

function PlayerManager.GetUnlockIds(player)
    return sessionData[player.UserId].UnlockIds
end

function PlayerManager.OnPlayerRemoving(player)
    SaveData(player, sessionData[player.UserId])
    playerRemoving:Fire(player)
end

function PlayerManager.OnClose()
    for _, player in ipairs(Players:GetPlayers()) do
        PlayerManager.OnPlayerAdded(player)
    end
end

return PlayerManager
1
Do I have to change the value/name of something? pls let me know Cowgato 33 — 18d

1 answer

Log in to vote
0
Answered by 17 days ago

Rename player data, this will reset data. It would then run a different data service.

Ad

Answer this question