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

[SOLVED] What is wrong with this Data Store Script?

I am trying to create a script that will save anything in a player's backpack when they leave and/or die, and load it back into their backpack when they return. Am I missing something here?

local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:connect(
function(Player)
    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
	local PlayerItems = PlayerStore:GetAsync("PlayerItems")
	if not PlayerItems then
		PlayerItems = Player.Backpack:GetChildren()
		PlayerStore:SetAsync("PlayerItems", PlayerItems)
	end
	

P.S. I am aware there isn't any code for when they die, because I don't know what I need to do for that.

5 comments

1

There should be a ) after the last end.

1

X| wow I missed that. thx! :)

0

no problem :P

1

But you need to find a different way to store the values. The items cannot be stored. The table needs to be strings, bools, or numbers

0

Please post an answer to your question if you solved it.

1 answer

✓ Accepted answer

I made this exact thing. Here's my code edited for your needs:

local key = 'weps'
local exclude = {
	'StarterSword';
}
-- I suggest placing the name of the initial sword here to
-- prevent dupes. Also place VIP tool(s) (if any) names here

game.Players.PlayerAdded:connect(
	function(p)

		local weps = data:GetAsync(p.userId..'_'..key)

		if weps then -- if it has already been saved
			for _,v in pairs(weps) do
				if game.ReplicatedStorage:FindFirstChild(v) then
					game.ReplicatedStorage[v]:clone().Parent = p.StarterGear
				end
			end
		end

		p.StarterGear.ChildAdded:connect(
			function(c)
				local tools = { }
				for _,v in pairs(p.StarterGear:GetChildren()) do
					local add = true
					for _,c in pairs(excluded) do
						if v.Name ~= c.Name then
							add = false
						end
					end
					if add then
						table.insert(mo, v.Name)
					end
				end
				data:SetAsync(p.userId..'_'..key),  tools) 
			end
		)
	end
)

The result is a table with the names of the tools and/or hopperbins.

Note: This script saves an the tools whenever an item is added to StarterGear. When giving a player a tool you want them to keep, I suggest placing it in their Backpack as well as their StarterGear. Doing so makes it so you don't need to keep re-adding the tools upon dying/respawning.

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