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
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.
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.
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.
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).