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