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

How do I prevent tools from disappearing from the player's backpack after death?

Asked by 5 years ago

I created a shop that sold guns, and it all worked fine. The thing is, when the player dies, they lose all the guns that they bought, and only the starter gun remains in their inventory.

Here's my LocalScript inside of one of the guns:

001game.Players.LocalPlayer.Backpack.ChildAdded:Connect(function()
002    local MaxAmmo = 40
003    local Ammo = MaxAmmo
004    local character = game.Players.LocalPlayer.Character
005    local function createBullet()
006        local bulletPart = workspace.bullet:Clone()
007        bulletPart.Parent = workspace
008        bulletPart.Position = script.Parent.Handle.Part.Position
009        bulletPart.Orientation = bulletPart.Orientation + (script.Parent.Handle.Orientation - Vector3.new(0,90,0))
010        for i = 0,25,10 do
011            bulletPart.CFrame = bulletPart.CFrame * CFrame.new(0,0,i)
012            wait()
013        end
014        bulletPart:remove()
015    end
View all 100 lines...

Yes, this is long and I'm very sorry for that. I really need answers though, because I want this game out as soon as possible.

**

0
do you have a core script for players? just wondering because my method involves cloning the players tools when they die, temporarly store them in a seperate folder, then when the character loads, I take those cloned tools and replace them in the backpack. mantorok4866 201 — 5y

1 answer

Log in to vote
0
Answered by
Nickzus 24
5 years ago
Edited 5 years ago

Hello,

The problem is not with the script in the gun, but with the script in the shop. One thing you can do to solve that is when you clone the gun part to the backpack of the player, you will need to also clone it to the starter gear, thus the item will persist when your player dies:

1local gun1 = game.ServerStorage.gun:Clone()
2local gun2 = game.ServerStorage.gun:Clone()
3 
4gun1.Parent = player:FindFirstChild("Backpack")
5gun2.Parent = player:FindFirstChild("StarterGear")

Sorry, I forgot to mention that you will need to create another global script in the server script storage like that:

01game.Players.PlayerAdded:Connect(function(plr)
02    local StarterGear = Instance.new("Folder")
03    StarterGear.Name = "StarterGear"
04    StarterGear.Parent = plr
05 
06    plr.CharacterAdded:connect(function()
07        local startItems = StarterGear:GetChildren()
08        for i=0,#startItems do
09            local itemToClone = startItems[i]:Clone()
10            itemToClone.Parent = plr.Backpack
11        end
12    end)   
13end)
0
It doesn't work! It just says that StarterGear doesn't exist..? Sensei_Developer 298 — 5y
0
Try it now Nickzus 24 — 5y
Ad

Answer this question