Cant give tools when die

I want to make custom sequence of tools. So i made a script.

local db = game:GetService("DataStoreService")
local dbweapons = db:GetDataStore("Weapons")

local rocketlauncher = game.ReplicatedStorage.Tools.RocketLauncher
local slingshot = game.ReplicatedStorage.Tools.Slingshot
local superball = game.ReplicatedStorage.Tools.Superball
local sword = game.ReplicatedStorage.Tools.Sword
local timebomb = game.ReplicatedStorage.Tools.Timebomb
local trowel = game.ReplicatedStorage.Tools.Trowel

local weapons = nil

local function giveWeapons(plr)
	if dbweapons:GetAsync("weapons"..plr.UserId) then
		
	else
		weapons = {sword,timebomb,rocketlauncher,superball,trowel,slingshot}
		for i, w in pairs(weapons) do
			w.Parent = plr.Backpack
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	giveWeapons(plr)
	plr.Character.Humanoid.Died:Connect(function()
		giveWeapons(plr) 
	end)
end)

Its giving tools when i play the game, but its not working when i die. How can i give it when character dies?

Instead of

plr.Character.Humanoid.Died:Connect(function()
		giveWeapons(plr) 
end)

use

plr.CharacterAdded:Connect(function(Character)
giveWeapons(plr)
end)

I know but i want to give it when character dies

1 Like

The reason it doesn’t work is because dying is not respawning. The player loses their items when they die. Give the weapons whenever the character is created. So everytime on spawn.
Try this:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
	    giveWeapons(plr)
	end)
end)
1 Like

Why? The character can’t access weapons when they’re dead.

1 Like

its not working again, it only gives when i join game :confused:

There’s 2 things wrong here.
Firstly, you should be using the CharacterAdded event as others have suggested here.
Secondly, you are simply moving the weapons in the game into the players backpack. This results in the weapons being taken out of ReplicatedStorage and moved into the backpack leaving no more weapons in ReplicatedStorage. To fix this, you need to create a copy of the weapon you want to give the player, and then move that copy into their backpack, not the original that is kept ReplicatedStorage.

local db = game:GetService("DataStoreService")
local dbweapons = db:GetDataStore("Weapons")

local rocketlauncher = game.ReplicatedStorage.Tools.RocketLauncher
local slingshot = game.ReplicatedStorage.Tools.Slingshot
local superball = game.ReplicatedStorage.Tools.Superball
local sword = game.ReplicatedStorage.Tools.Sword
local timebomb = game.ReplicatedStorage.Tools.Timebomb
local trowel = game.ReplicatedStorage.Tools.Trowel

local weapons = nil

local function giveWeapons(plr)
	if dbweapons:GetAsync("weapons"..plr.UserId) then
		
	else
		weapons = {sword,timebomb,rocketlauncher,superball,trowel,slingshot}
		for i, w in pairs(weapons) do
			local cloned_weapon = w:Clone() -- clone the weapon in the table
			cloned_weapon.Parent = plr.Backpack -- move that clone into the players backpack
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function() -- give the player weapons when their character is spawned/respawned
		giveWeapons(plr) 
	end)
end)
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.