Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Keep items in the backpack, even when player dies?

Asked by
MexheCo 46
6 years ago

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):

01leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
02 
03function onClickEvent()
04 
05    print(game.Players.LocalPlayer.Name.." is attempting a purchase.")
06 
07    if leaderstats.Tokens.Value < 0 then
08 
09        print("Not enough tokens!")
10        script.Error:Play()
11 
12    else
13 
14        print("Ok!")
15        leaderstats.Tokens.Value = leaderstats.Tokens.Value - 1500
View all 25 lines...

4 answers

Log in to vote
0
Answered by 6 years ago

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

01-- server script in ServerScriptService
02 
03local boughtItems = {
04    game.ServerStorage.Items.Item1;
05    game.ServerStorage.Items.Item2;
06}
07 
08game:GetService("Players").PlayerAdded:Connect(function(p)
09    p.CharacterAdded:connect(function()
10        for a,b in next, boughtitems do
11            b:Clone().Parent=p:WaitForChild("Backpack")
12        end
13    end)
14end)
0
This answer just needs to extend in knowing which player owns which items. xPolarium 1388 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

01leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
02 
03function onClickEvent()
04 
05    print(game.Players.LocalPlayer.Name.." is attempting a purchase.")
06 
07    if leaderstats.Tokens.Value < 0 then
08 
09        print("Not enough tokens!")
10        script.Error:Play()
11 
12    else
13 
14        print("Ok!")
15        leaderstats.Tokens.Value = leaderstats.Tokens.Value - 1500
View all 26 lines...

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.

0
This will have replication issues. xPolarium 1388 — 6y
Log in to vote
0
Answered by 6 years ago
01local savedBackpack = {} -- Creating a table for our backpack, we'll iterate through this later.
02local Player = game:GetService("Players").LocalPlayer
03 
04-- When the user purchases an item,
05 
06local purchasedItem = game.ReplicatedStorage.Items.ClassicLauncher:Clone()
07purchasedItem.Parent = game.Players.LocalPlayer:WaitForChild("Backpack")
08table.insert(savedBackpack, purchasedItem);
09 
10-- When the player dies,
11 
12for _,v in pairs(savedBackpack) do
13    v.Parent = Player:WaitForChild("BackPack")
14end

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

0
This will have replication issues. Cloning to the backpack won't replicate to server. Nothing wrong with using Storage services. xPolarium 1388 — 6y
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. NodeSupport 32 — 6y
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. NodeSupport 32 — 6y
Log in to vote
0
Answered by 6 years ago
01--put in ServerScriptService
02local Storage = Instance.new("Folder")
03Storage.Parent = game:GetService("ServerStorage")
04Storage.Name = "Storage"
05game:GetService("Players").PlayerAdded:connect(function(plr)
06    if not Storage:FindFirstChild(plr.Name) then --Creates player storage
07        local Folder = Instance.new("Folder")
08        Folder.Parent = Storage
09        Folder.Name = plr.Name
10    end
11    plr.CharacterAdded:connect(function(char)
12        for i,v in pairs(Storage[plr.Name]:GetChildren()) do --Loads player data
13            v.Parent = char
14        end
15        char.Humanoid.Died:connect(function()
View all 28 lines...

Answer this question