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

How do you save tools using DataStore2?

Asked by 4 years ago
Edited 4 years ago

Hello there.


I am trying to save tools with DataStore2. I’ve tried doing it myself, asked on the devforums, and I’ve also searched it up on YouTube to no avail.


Leaderstats Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local Votes = ReplicatedStorage:WaitForChild("VotesEnableDisable")

    local DataStore2 = require(script.Parent.DataStore2)

    DataStore2.Combine("DATA", "Wins", "Tools")

    game:GetService("Players").PlayerAdded:Connect(function(Player)
        local WinsStore = DataStore2("Wins", Player)
        local ToolStore = DataStore2("Tools", Player)

        local Tools = {}

        game:GetService("ReplicatedStorage").PlayerAdded:FireAllClients()

        Player.CharacterAdded:Connect(function(Character)
            Character.Humanoid.Died:Connect(function()
                ReplicatedStorage.PlayerDied:FireAllClients(Player.Name)
            end)
        end)    

        local Leaderstats = Instance.new("Folder")
        Leaderstats.Name = "leaderstats"

        local Wins = Instance.new("IntValue")
        Wins.Name = "Wins"
        Wins.Value = WinsStore:Get(0)
        Wins.Parent = Leaderstats

        local InRound = Instance.new("BoolValue")
        InRound.Name = "InRound"
        InRound.Parent = Player

        local Afk = Instance.new("BoolValue")
        Afk.Name = "Afk"
        Afk.Parent = Player

        WinsStore:OnUpdate(function(NewWins)
            Wins.Value = NewWins
        end)

        ToolStore:OnUpdate(function(ToolName)
            table.insert(Tools, 1, ToolName)
        end)

        Leaderstats.Parent = Player

        for _, ToolName in pairs(Tools) do
            ReplicatedStorage[ToolName]:Clone().Parent = Player.Backpack
        end
    end)

Shop Handler:

local BuyEvent = game:GetService("ReplicatedStorage"):WaitForChild("BuyItem")

local DataStore2 = require(script.Parent.DataStore2)

BuyEvent.OnServerEvent:Connect(function(Player, ToolName)
    local ToolStore = DataStore2("Tools", Player)

    local Tool = game:GetService("ServerStorage"):FindFirstChild(ToolName)

    if Tool then
        if Player.leaderstats.Wins.Value >= script[ToolName].Value and not Player.StarterGear:FindFirstChild(ToolName) then
            Tool:Clone().Parent = Player.StarterGear
            Tool:Clone().Parent = Player.Backpack

            ToolStore:Set(ToolName, false)
        end
    else
        Player:Kick("No exploiting please.")
    end
end)

(This is a repost because my previous post got moderated.)

0
save on leave and bindtoclose and make a table of the names of the tools and when u join loop through the table of the names and clone them in HappyTimIsHim 652 — 4y
0
@HappyTimIsHim I'm talking about DataStore2, a module, not normal DataStore. youtubemasterWOW 2741 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

The best thing is to keep a tool folder in ReplicatedStorage to make it look neat to prevent other instances being looked up accidentally. You haven't parented the tools to Player.StarterPack.

0
Thanks, but it did not work. youtubemasterWOW 2741 — 4y
0
In the shop handler, you rewrite the tool data store to a string, you need to get the table and insert the toolName then set it.. @youtubemasterWOW FunctionalMetatable 490 — 4y
Ad

Answer this question