Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you save tools using DataStore2?

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.)

2 comments

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

0

@HappyTimIsHim I'm talking about DataStore2, a module, not normal DataStore.

1 answer

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.

2 comments

0

Thanks, but it did not work.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.