Hello!

Question: How do I get this DataStore to retain the items when a player dies, or leaves the game and returns?

Information: I'm having some problems getting my DataStore to save purchased items. I was receiving help before, but I still have problems with it.

Here's how I have the DataStore process laid out:

I have a BoolValue inside PlayerGui called "HasM9". The Shop purchase script turns the Value to True like so:


--Shop Script Above
if plr.leaderstats.Points.Value >= amount then 	
tool:Clone().Parent = plr.Backpack
tool:Clone().Parent = plr.StarterGear
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - amount 
game.StarterGui.HasM9.Value = true
game.Players.LocalPlayer.PlayerGui.HasM9.Value = true
--Shop Script Below

Player is able to purchase the Item. However, after the player dies/respawns, the item is gone from their Backpack. Here's my Data Store:


game.Players.PlayerAdded:connect(function(player)
    local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    player:WaitForChild("leaderstats")
    wait(1)
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do           
    stats[i].Value = datastore:GetAsync(stats[i].Name)
    print("stat number "..i.." has been found")

--  ITEM SAVING BELOW
local DataStorem9 = game:GetService("DataStoreService"):GetDataStore("HasM9")
    local HasM9 = DataStorem9:GetAsync(player.userId)
    if HasM9 == true then
    game.ServerStorage.ToolStorage.M9:Clone().Parent = player.Backpack
end


player.PlayerGui:WaitForChild("HasM9").Changed:Connect(function()
if player.PlayerGui.HasM9.Value == true then
local DataStorem9 = game:GetService("DataStoreService"):GetDataStore("HasM9")
DataStorem9:SetAsync(player.userId, true)
end
end)

--  ITEM SAVING ABOVE

    stats[i].Changed:Connect(function()
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
    datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
    print("saved data number "..i)
end
print("Stats successfully saved")  
end)
    end
end)

How do I get this DataStore to retain the items when a player dies, or leaves the game and returns?

I wondered if my Global Death Script and Global Leave Script might be interfering? The scripts are from AlvinBlox's Rounds tutorial.

Global Death Script:

game:GetService('Players').PlayerAdded:connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild('Humanoid').Died:connect(function()
			local Update = {}
			for i,v in pairs(_G.gameplayers) do
				if v ~= player.Name then
					table.insert(Update, 1,v)
				end
			end
			_G.gameplayers = Update
		end)
	end)
end)

Global Leave Script:

game:GetService('Players').PlayerRemoving:connect(function(player)
	
			local Update = {}
			for i, v in pairs(_G.gameplayers) do
				if v ~= player.Name then
					table.insert(Update, 1,v)
				end
				
			end
			_G.gameplayers = Update
end)

My apologies for this big block of text. :( I really want to get this Data Store problem solved to get my game up and running for the public.

2 comments

0

I would recommend not loading the datastore every time someone dies. This can lead to them sometimes not getting the item due to a failed operation, going over the limit in some cases and a substantial delay between spawning and getting their item. Instead just load it when they enter and store it in a central table and check that when they die/update it when they purchase something.

0

Also remember to use pcall on all datastore operations so that your script wont break