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.
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.
Locked by TheMyrco
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?