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

Keep items in the backpack, even when player dies?

So the player will purchase an item from the shop, which needs to be given to the player in such a way that it will stay in the backpack, even if they reset.

Backpack doesn't work, because it resets when the player dies.

Also, StarterGear doesn't work, because then every player will get the item.

I'm stumped...

Here's my code (temporarily using the Backpack):

leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

function onClickEvent()
	
	print(game.Players.LocalPlayer.Name.." is attempting a purchase.")
	
	if leaderstats.Tokens.Value < 0 then
		
		print("Not enough tokens!")
		script.Error:Play()
		
	else
		
		print("Ok!")
		leaderstats.Tokens.Value = leaderstats.Tokens.Value - 1500
		
		local purchasedItem = game.ReplicatedStorage.Items.ClassicLauncher:Clone()
		
		purchasedItem.Parent = game.Players.LocalPlayer:WaitForChild("Backpack")
		
	end
	
end

script.Parent.MouseButton1Click:Connect(onClickEvent)

4 answers

I suggest you :Clone() items from ServerStorage into the player's backpack after they die, aka when their character loads.

-- server script in ServerScriptService

local boughtItems = {
	game.ServerStorage.Items.Item1;
	game.ServerStorage.Items.Item2;
}

game:GetService("Players").PlayerAdded:Connect(function(p)
	p.CharacterAdded:connect(function()
		for a,b in next, boughtitems do
			b:Clone().Parent=p:WaitForChild("Backpack")
		end
	end)
end)

1 comment

0

This answer just needs to extend in knowing which player owns which items.

You should clone the tool to the backpack, plus another one to THAT players starter gear.

leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

function onClickEvent()

    print(game.Players.LocalPlayer.Name.." is attempting a purchase.")

    if leaderstats.Tokens.Value < 0 then

        print("Not enough tokens!")
        script.Error:Play()

    else

        print("Ok!")
        leaderstats.Tokens.Value = leaderstats.Tokens.Value - 1500

        local purchasedItem = game.ReplicatedStorage.Items.ClassicLauncher:Clone()
local purchasedItem2 = game.ReplicatedStorage.Items.ClassicLauncher:Clone()
        purchasedItem.Parent = game.Players.LocalPlayer:WaitForChild("Backpack")
purchasedItem2.Parent = game.Players.LocalPlayer:WaitForChild("StarterGear")

    end

end

script.Parent.MouseButton1Click:Connect(onClickEvent)

and btw, you should do these things on client side. Only the client would see the leaderstats change. You should use remote events to make it seen to the server.

1 comment

0

This will have replication issues.

local savedBackpack = {} -- Creating a table for our backpack, we'll iterate through this later.
local Player = game:GetService("Players").LocalPlayer

-- When the user purchases an item,

local purchasedItem = game.ReplicatedStorage.Items.ClassicLauncher:Clone()
purchasedItem.Parent = game.Players.LocalPlayer:WaitForChild("Backpack")
table.insert(savedBackpack, purchasedItem);

-- When the player dies,

for _,v in pairs(savedBackpack) do
	v.Parent = Player:WaitForChild("BackPack")
end

Please for the love of god use tables for this, don't parent anything to a storage like others have sugguested.

3 comments

0

This will have replication issues. Cloning to the backpack won't replicate to server. Nothing wrong with using Storage services.

1

@xPolarium, let me start off by saying how misinformed you are. Replicating to a backpack when calling from a ServerScript will replicate to the entire server. The only time it wont is when you are requesting the items from a local script. This script that I provided was split in to multiple selections as seen via comments. Everything is wrong with using ServerStorage for something like this.

1

Cloning objects to ServerStorage rather then storing them in a signified thread per user will result in way to many extra objects and unnecessary organization. Your logic is that every time a user dies put there tools inside of a folder in the Storage and then move them back when the player respawns. I don't know why anyone would even think that'd be a good idea. Please refrain from commenting.

--put in ServerScriptService
local Storage = Instance.new("Folder")
Storage.Parent = game:GetService("ServerStorage")
Storage.Name = "Storage"
game:GetService("Players").PlayerAdded:connect(function(plr)
	if not Storage:FindFirstChild(plr.Name) then --Creates player storage
		local Folder = Instance.new("Folder")
		Folder.Parent = Storage
		Folder.Name = plr.Name
	end
	plr.CharacterAdded:connect(function(char)
		for i,v in pairs(Storage[plr.Name]:GetChildren()) do --Loads player data
			v.Parent = char
		end
		char.Humanoid.Died:connect(function()
			for i,v in pairs(char:GetChildren()) do --Saves player data
				if v:IsA("Tool") then
					v.Parent = Storage
				end
			end
		end)
	end)
end)
game:GetService("Players").PlayerRemoving:connect(function(plr)
	pcall(function()
		Storage[plr.Name]:Destroy() --Removes player data
	end)
end)

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