When i`m died, my tool disappears

When i`m died, my tool disappears…

Hi, I’m making a sword with a script for my game, it’s pretty simple. That’s what the sword itself is, I go to the store, and I open Gui for sale I choose the sword and buy it, the sword is claniruetsya to me in the inventory (the Server and the client see this sword).

But there is one problem, when a player dies, the sword just disappears!

The problem is in the Character. If I use local Character = Player.Character or Player.CharacterAdded:Wait() then everything works, but if I die, then the sword will just disappear. If I use Player.CharacterAdded:Connect(function(Character) then the sword stops working at all. Please help.

(I have local script in my sword)

Script -

local Debounce = false
local CanAttack = false
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Sword = script.Parent.Parent
local HixBox = script.Parent
local SwordEquippedSound = Sword:WaitForChild("SwordEquippedSound")
local SwordSmashSound = Sword:WaitForChild("SwordSmashSound")
local ToolSmashAnimationR15 = Sword:WaitForChild("SmashAnimationR15")
if Humanoid.Health > 0 then 
local SmashAnimationR15 = Humanoid:LoadAnimation(ToolSmashAnimationR15)

Sword.Equipped:Connect(function(Equipped)
	if Humanoid.Health > 0 then
		SwordEquippedSound:Play()
	end
end)

Sword.Unequipped:Connect(function(UnEquipped)
	if Humanoid.Health > 0 then
		SmashAnimationR15:stop()
		SwordEquippedSound:Stop()
		SwordSmashSound:Stop()
	end
end)

Sword.Activated:Connect(function(Activated)
	if Humanoid.Health > 0 then
		if Sword.Equipped and Debounce == false then
			SmashAnimationR15:Play()
					SwordSmashSound:Play()
					CanAttack = true
					Debounce = true
					wait(0.875)
					Debounce = false
					CanAttack = false
				end	
			end
		end)
HixBox.Touched:Connect(function(Hit)
	if CanAttack == true then
		if Humanoid.Health > 0 then
			local HumanoidHit = Hit.Parent:FindFirstChild("Humanoid")
				if HumanoidHit then
					HumanoidHit:TakeDamage(12.5)
					CanAttack = false
					wait(0.875)
					CanAttack = true
				end	
			end
		end		
	end)
end
1 Like

This might not be the simplest way to do it but:

  1. create a folder in the user called something like “BoughtTools” and when they buy (for example) a sword it puts a bool value in the folder named the name of the item.

  2. Detect when the player dies then when the player dies you can loop through the tools in the “BoughtTools” folder in the user and give them the tools again.

Hope this makes sense?

3 Likes

I’ll try it.(If it helps, I will answer)

1 Like

What if you place it in the StarterPack? I’ve put tools there and your character spawns with them. I don’t know if it will give you the tool immediately when you buy it though.

If I put an item in the starter pack when I buy a sword, they won’t appear in my backpack, I tried this.

That wouldn’t work for the use here. StarterPack gives it immediately whereas this is a shop type thing if I am not mistaken.

OP wants it so AFTER they have bought it it gives it to them once they have died instead of automatically having it.

https://developer.roblox.com/en-us/articles/intro-to-player-tools

Is there a way to clone an item via local script? or only via server script?

When a player buys a tool, clone the tool into Player.StarterGear, so that when they die, they’ll get the tools they bought after respawning and other players won’t get the tools they purchased (referring to StarterPack behavior).

It was difficult and I was very confused, but now everything works, thank you.

It was difficult and I was very confused, but now finaly everything works, thank you.

1 Like

Best way of doing it and the simplest, Clone the bought item 2 times, First clone goes in the backpack the second goes into startergear.

Example

local Clone1 = Sword:Clone()
Clone1.Parent = player.Backpack
local Clone2 = Sword:Clone()
Clone2.Parent = player.StarterGear

@NexorTop

1 Like

Just realized it got fixed well anyways you can try both methods.

You could make a folder with all the tools that the player buyed then just do this:

local Tools = game.ReplicatedStorage.BuyedTools
game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:FindFirstChild("Humanoid")
        Humanoid.Died:Connect(function()
            Tools:GetChildren().Parent = game.StarterPack
        end)
    end)
end)

He could but whats more simple adding it to the StarterGear or making a new folder, You know what I mean.

I know it’s been a long time, but you were right. What you suggested really works and it’s much better