How to use DataStore2 - Data Store caching and data loss prevention

Hello everyone, my name is InquistorWasBanned and I am making a DataStore 2 that would save the gold that a player has. This gold should go up by 150 whenever you kill someone. I already made a DataStore2 that saves the amount but I can not seem to get it to save the gold amount if a player kills someone for gold. Is there a way to just set up a DataStore2 when I already made a different script that has my leaderstats? So would there be a way to call for example the Gold and save it instead of having to add the kill player for money script inside of the same script?

Thanks!

I think datastore2 only used Increment() so use it if you use gold = gold + 150

Not true. Increment is just a helper function, it’s never required.

@InquistorWasBanned I don’t understand your problem. In your script that handles players killing each other, just increment their gold store there.

@Kampfkarren , could you help me please, this is what I got so far:

local DataStore2 = require(1936396537)
game.Players.PlayerAdded:Connect(function(plr)
	print("Player Added")
	local dataweap = DataStore2("Weapons",plr)
	
	local fol = Instance.new("Folder")
	fol.Parent = plr
	fol.Name = "Weapons"
	local tbl = {}
	print("Folder Added")
	if dataweap:Get() ~= nil then
		for i,v in pairs(dataweap:Get())do
			table.insert(tbl,v)
			local tag = Instance.new("BoolValue")
			tag.Parent = fol
			tag.Name = v
		end
	else
		table.insert(dataweap,"Wooden Bow")
		table.insert(tbl,"Wooden Bow")
		local tag = Instance.new("BoolValue")
		tag.Parent = fol
		tag.Name = "Wooden Bow"
		table.insert(dataweap,"Wooden Sword")
		table.insert(tbl,"Wooden Sword")
		local tag = Instance.new("BoolValue")
		tag.Parent = fol
		tag.Name = "Wooden Sword"
		table.insert(dataweap,"Wooden Spear")
		table.insert(tbl,"Wooden Spear")
		local tag = Instance.new("BoolValue")
		tag.Parent = fol
		tag.Name = "Wooden Spear"
		dataweap:Set(tbl)
	end
	
	local function tblupdate(new)
		if dataweap:Get()then
			for i,v in pairs(dataweap:GetChildren())do
				print(v)
				if game:GetService("ReplicatedStorage").Weapons:FindFirstChild(v) then	
					if plr.Weapons:FindFirstChild(v)then
					else
						table.insert(tbl,v)
						local tag = Instance.new("BoolValue")
						tag.Parent = fol
						tag.Name = v
					end
				else
					table.remove(tbl,v)
					table.remove(dataweap,v)
				end 
			end
		end
		dataweap:Save(tbl)
	end
	dataweap:OnUpdate(tblupdate())
end)

I’m trying to use datastore2 to save weapons name of the player. I inserted boolvalues in the folder so I could access the player’s inventory later in the game. Could you help me fix this please, the data didn’t load up and there was no new folders in the player after I tested even though I put a boolvalue name “SaveInStudio” in the server storage.

This is my script how would I combine my kill for money script inside of my DataStore 2?


DataStore2.Combine("MasterKey","Gold")

game.Players.PlayerAdded:Connect(function(plr)
	local dataGold = DataStore2("Gold", plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"
	
	if dataGold:Get() ~= nil then
		gold.Value = dataGold:Get()
	else
		gold.Value = 100
	end
	
	gold.Changed:Connect(function()
		dataGold:Set(gold.Value)
	end)
	
end)
---This is the code that is using DataStore2 How would I add or plug in my Kill for Money Script on the bottom?---
game.Players.PlayerAdded:connect(function(player)

 local folder = Instance.new("Folder",player)

 folder.Name = "leaderstats"

 local currency = Instance.new("IntValue",folder)

 currency.Name = "Gold"

 player.CharacterAdded:connect(function(character)

  character:WaitForChild("Humanoid").Died:connect(function()

  local tag = character.Humanoid:FindFirstChild("creator")

   if tag ~= nil then

    if tag.Value ~= nil then

 local stats = tag.Value:WaitForChild("leaderstats")

stats["Gold"].Value = stats ["Gold"].Value + 150


    

    end

   end

  end)

 end)

end)

This isn’t how you use :Save. :Set updates the data, not :Save. Also, you should not be calling :Save this often–you will throttle extremely fast.

This is not how you use OnUpdate. Change this to dataweap:OnUpdate(tblupdate).

Please review the documentation.

1 Like

From a glance, this looks correct since you are changing the gold on Changed.

1 Like

I have two scripts so how would I plug the kill script in my datstore

When I use both scripts it gets mixed up a little I think, because it is the same leaderstats name, Gold. Maybe you did not see my kill player for money script or would it work?

Your issue may have to do with the timing of when you are adding the “creator” tag to the humanoid of the player that has been killed. Currently, the tag is expected to be parented to the humanoid before the humanoid.Died event fires. Nothing will happen if the tag is parented to the humanoid after the humanoid.Died fires.

Alternately, just change the leaderstats “Gold” value from your kill script. Since you are listening to the changed event, the data should update when you change your leaderstats value.

That is right, but the killing script works fine, the only problem is its both the same name so its stuck in between. So I would like to replace my DataStore part where it names the leaderstats and add my kill script there, but its not working.

Why Is this Script Not Working Can Anyone help me Fix This? It is supposed to save the gold and renew every time and give gold per kill you kill a person, 150 gold


DataStore2.Combine("MasterKey","Gold")

game.Players.PlayerAdded:Connect(function(plr)
	local dataGold = DataStore2("Gold", plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"
	player.CharacterAdded:connect(function(character)
		
		character:WaitForChild("Humanoid").Died:connect(function()
			
			local tag = character.Humanoid:FindFirstChild("creator")
			
			if tag ~= nil then
				
				if tag.Value ~= nil then
					
					local stats = tag.Value:WaitForChild("leaderstats")
					
					stats["Gold"].Value = stats ["Gold"].Value + 150
	if dataGold:Get() ~= nil then
		gold.Value = dataGold:Get()
	else
		gold.Value = 100
	end
	
	gold.Changed:Connect(function()
		dataGold:Set(gold.Value)
	end)
	
end
---Why is this not working can anyone help fix this?

Datastore2 basically just stores the whole history of player data right?

Your Players.PlayerAdded event is passing the parameter plr while your CharacterAdded event is attached to player:

You are initializing dataGold inside the humanoid.Died event. Move it to the PlayerAdded function. You should also be passing an initial value to the Get method rather than using the if statement:

local initialGold = 100
gold.Value = dataGold:Get(initialGold)
1 Like

If you are using the OrderedBackups saving method (the “berezaa method”), then yes. For more info, see here:

1 Like

@Grargror I understand what you mean but how would I replace it because my whole death function requires on the character function and where would I put the functions of gold you said?


game.Players.PlayerAdded:Connect(function(plr)
	local dataGold = DataStore2("Gold", plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"
	
		
		character:WaitForChild("Humanoid").Died:connect(function()
			
			local tag = character.Humanoid:FindFirstChild("creator")
			
			if tag ~= nil then
				
				if tag.Value ~= nil then
					
					local stats = tag.Value:WaitForChild("leaderstats")
					
					stats["Gold"].Value = stats ["Gold"].Value + 150
					if dataGold:Get() ~= nil then
						gold.Value = dataGold:Get()
					else
						gold.Value = 100
					end
					
					gold.Changed:Connect(function()
						dataGold:Set(gold.Value)
					end)
					
				end

You should be initializing stuff as indicated in the following code snippet:

game:GetService("Players").PlayerAdded:Connect(function(player)
	-- This is your PlayerAdded event function.
	-- Initialize your datastore and leaderstats here
	
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			-- This is your Humanoid.Died event function
			-- Handle the died event here
		end)
	end)
end)

So Like that? Would this work?

game.Players.PlayerAdded:Connect(function(plr)
	local dataGold = DataStore2("Gold", plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"
	if dataGold:Get() ~= nil then
		gold.Value = dataGold:Get()
	else
		gold.Value = 100
	end
	
	gold.Changed:Connect(function()
		dataGold:Set(gold.Value)
	
	player.CharacterAdded:Connect(function(character)
	character:WaitForChild("Humanoid").Died:connect(function()
		
		local tag = character.Humanoid:FindFirstChild("creator")
		
		if tag ~= nil then
			
			if tag.Value ~= nil then
				
				local stats = tag.Value:WaitForChild("leaderstats")
				
				stats["Gold"].Value = stats ["Gold"].Value + 150
				
				end
				
			end

This is how I do it using Datastore2

local cool = false
script.Parent.Touched:Connect(function(hit)
	if cool == false and hit.Parent:FindFirstChildWhichIsA("Humanoid") and hit.Parent ~= script.Parent.Parent.Parent then
		cool = true
		hit.Parent:FindFirstChildWhichIsA("Humanoid").Health = hit.Parent:FindFirstChildWhichIsA("Humanoid").Health - 20
                    -- Damage Indicator
		local text = game.ReplicatedStorage.GameCrops.Dam:Clone()
		text.TextLabel.Text = "-20"
		text.TextLabel.TextColor3 = Color3.new(1, 0.67, 0.3)
		text.TextLabel.TextSize = 40
		text.Parent = hit.Parent.Head
		text.Script.Disabled = false
                    -- Stuff for the leaderboard
		if hit.Parent:FindFirstChildWhichIsA("Humanoid").Health <= 0 then
			local name = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
			if name then
				local DataStore2 = require(1936396537)
				local datakill = DataStore2("Kills", name)
				local datagold = DataStore2("Golds", name)
				datakill:Increment(1)
				datagold:Increment(2)
			end
		end
		wait(1)
		cool = false
	end
end)

I put this script in a blade of a sword tool.

That should work once you’ve done the following:

  1. Fix the formatting errors. Make sure parentheses are closed and functions all have an “end” keyword.
  2. Replace the “if” statement, and properly initialize the dataGold variable:
-- Replace this:
if dataGold:Get() ~= nil then
    gold.Value = dataGold:Get()
else
	gold.Value = 100
end

-- With this:
local initialGold = 100
gold.Value = dataGold:Get(initialGold)