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

data store with tools not working as intended?

Asked by
0msh 333 Moderation Voter
5 years ago

In the game I'm working on there's a rebirth feature where after the player rebirth, certain stats and all the tools will be removed (tools are saved in StarterGearand I use StarterGear:ClearAllChildren() to remove all the tools the player own).

After I rebirth, tools are gone from my StarterGear, but after I rejoined the game, the tools I have before I rebirth came back (when it shouldn't) so I thought maybe it's a problem with my data store. Please help.

serv = game:GetService("DataStoreService")
local Stats = serv:GetDataStore("StatStore")
local Tools = serv:GetDataStore("ToolStore")

LoadData = function(plr,level,exp)
    -- Stats
    pcall(function()
        local GetData = Stats:GetAsync(plr.UserId)
        if GetData then
            level.Value = GetData[1]
            exp.Value = GetData[2]
        else
            level.Value = tonumber(nil)
            exp.Value = tonumber(nil)
        end
        -- Tools
        local ActualTools = {}
        local ToolStorage = game.ReplicatedStorage.Items
        if (Tools:GetAsync(plr.UserId)) then
            for _,v in pairs(Tools:GetAsync(plr.UserId)) do
                print(v)
                if ToolStorage:FindFirstChild(v) then
                    table.insert(ActualTools, v)
                end
            end
            for _,v in pairs(ActualTools) do
                ToolStorage:FindFirstChild(v):Clone().Parent = plr:WaitForChild("StarterGear")
                ToolStorage:FindFirstChild(v):Clone().Parent = plr:WaitForChild("Backpack")
            end
        end
        local DataLoadBoolean = Instance.new("BoolValue")
        DataLoadBoolean.Name = "Loaded"
        DataLoadBoolean.Parent = plr
    end)
end

SaveData = function(plr)
    -- Stats
    pcall(function()
        local lstats = plr:FindFirstChild("leaderstats")
        if (lstats~=nil) then
            local level = lstats:FindFirstChild("lol")
            local exp = lstats:FindFirstChild("Rebirth")
            if (level~=nil) and (exp~=nil) then
                Stats:SetAsync(plr.UserId,{level.Value,exp.Value})
            end
        end
        -- Tools
        local ActualTools = {}
        local SG = plr:WaitForChild("StarterGear")
        for _,v in pairs(SG:GetChildren()) do
            table.insert(ActualTools, v.Name)
        end
        if ActualTools[1] then
            Tools:SetAsync(plr.UserId,ActualTools)
            print("data saved")
        end
    end)
end

game.Players.PlayerAdded:Connect(function(plr)

    local CanAutoSave = true
    local lstats = Instance.new("Folder",plr)
    lstats.Name = "leaderstats"

    local level = Instance.new("DoubleConstrainedValue",lstats)
    level.Name = "Weight"
    level.MaxValue = math.huge

    local exp = Instance.new("DoubleConstrainedValue",lstats)
    exp.Name = "Rebirth"
    exp.MaxValue = math.huge

    wait()

    pcall(function()
        LoadData(plr,level,exp)
    end)

        plr.Chatted:Connect(function(str)
            if str == ";save" then
                if CanAutoSave == true then
                    CanAutoSave = false
                    SaveData(plr)
                    wait(3*60)
                    CanAutoSave = true

        end
            end
            end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    if (not plr) then return end
    pcall(function()
            SaveData(plr)
    end)
end)

local ServerTimeout = 3
game:BindToClose(function()
    for _,v in pairs(game.Players:GetPlayers()) do
        if (v~=nil) then SaveData(v) end
    end
    wait(ServerTimeout) 
end)
0
In which line (Or block) of code are the tools saved in the data store? DragRacer31 7 — 5y
0
line 49-56. the problem is, it keeps on loading the tools I have after I cleared them if you know what I mean 0msh 333 — 5y
0
if your removing the tools via LocalScript then the tools are still there for the server the8bitdude11 358 — 5y
0
no I checked in the studio and with dev console in game, the tools are not there before I save/exit out the game then when I rejoin, it just magically appear 0msh 333 — 5y

Answer this question