When a player dies their tools go away from backpack

Hello, Can someone please help me! Okay so the problem is that when the player dies the tools are not in there backpack also this tool is for my shop!

Heres the code:

game.ReplicatedStorage.ToolEvents.GrenadeEvent.OnServerEvent:Connect(function()player)
if player.leaderstats.Cash.Value >= 50 then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50
game.StarterPack.Tools.Grenade:Clone().Parent = player.Backpack
end
end)

This is whats not working for me:

game.StarterPack.Tools.Grenade:Clone().Parent = player.Backpack

Can someone help me so that when the player resets/dies they still have the tool?

1 Like

Uh

You forgot to encase the player parameter inside the function, also couldn’t you just clone it into the Player’s StarterGear?

game.ReplicatedStorage.ToolEvents.GrenadeEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 50 then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50
		game.StarterPack.Tools.Grenade:Clone().Parent = player.Backpack
        game.StarterPack.Tools.Grenade:Clone().Parent = player.StarterGear
	end
end)
1 Like

You can Add A Remote Event In ReplicatedStorage and Call It ReToolGive.
This should be A LocalScript in StarterPlayer → StarterPlayerScripts

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character -- Waits Until Character Loads In
local Humanoid = Player.Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
  game.ReplicatedStorage.ReToolGive:FireServer() -- Fire the Remote
end)

Now This Should be A Script In ServerScriptService.

game.Players.PlayerAdded:Connect(function(Player)
   local Folder = Instance.new("Folder")
   Folder.Name = Player.Name .. "Tools"
   Folder.Parent = game.ServerStorage
end)

game.ReplicatedStorage.ReToolGive.OnServerEvent:Connect(function(Player)

   for i, v in pairs(Player:WaitForChild("Backpack"):GetChildren()) do
       if v:IsA("Tool") then 
          v.Parent = game.ServerStorage:FindFirstChild(Player.Name .. "Tools")  -- Parents to A Folder
      end
   end

  for i, v in pairs(Player.Character:GetChildren()) do
       if v:IsA("Tool") then 
          v.Parent = game.ServerStorage:FindFirstChild(Player.Name .. "Tools")  -- Parents to A Folder
      end
   end
 
   Player.CharacterAdded:Connect(function() -- Respawns
       for i, v in pairs(game.ServerStorage:FindFirstChild(Player.Name .. "Tools"):GetChildren()) do
            v.Parent = Player:WaitForChild("Backpack") -- Parents it Again
       end
    end)

 end)
1 Like

As @JackscarIitt stated, your syntax is incorrect when defining the function.

You might also run into the issue where the tool gets cloned into the character’s backpack automatically with it being in StarterPack.

I would reccomend putting the tool either into server storage and getting it from there, or as a child of this script.

I have programmed the code below to work if the tool is set as a child to the script.

game.ReplicatedStorage.ToolEvents.GrenadeEvent.OnServerEvent:Connect(function(player)
if player.leaderstats.Cash.Value >= 50 then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50
Script.Grenade:Clone().Parent = player.Backpack
end
end)

If you run into any issues with this let me know!

@YT_Forceman So now the problem is I have other tools with the player!

Which fixes the Grenade problem but now these tools in StarterPack get multiplied!

And @JackscarIitt
With that line that didn’t work either!

Those tools that are not in the folder are going to be when the player joins and have in there backpack/inventory

Isn’t there player.StarterGear too?

Clone it to the StarterGear too, not only backpack so when a player dies it will save?

DevHub:

And if you move all the tools to be bought to ServerStorage?

game.ReplicatedStorage.ToolEvents.GrenadeEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 50 then
		player.leaderstats.Cash.Value -= 50
		game:GetService("ServerStorage").Tools.Grenade:Clone().Parent = player.Backpack
		game:GetService("ServerStorage").Tools.Grenade:Clone().Parent = player.StarterGear
	end
end)
1 Like

Instead of doing:

game.StarterPack.Tools.Grenade:Clone().Parent = player.Backpack

do:

game.StarterPack.Tools.Grenade:Clone().Parent = player.StarterGear

1 Like

I’d just reference cloning the Tools inside ServerStorage instead, you can still put them inside StarterPack but make sure to duplicate a pack & put it in ServerStorage

game.ReplicatedStorage.ToolEvents.GrenadeEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 50 then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50
		game.ServerStorage.Tools.Grenade:Clone().Parent = player.Backpack
	end
end)

First, you should make a folder in ReplicatedStorage that contains all of the tools in your game.

Then, change the script to have this:

local tool = game.ReplicatedStorage.ToolsFolder.Grenade:Clone()

tool.Parent = player.Backpack

local savedTool = game.ReplicatedStorage.ToolsFolder.Grenade:Clone()

savedTool.Parent = player.StarterGear

Thank you @SOTR654 :smiley: your the best!!

1 Like

You’re welcome, happy to help ^ - ^

1 Like