Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why this only drop tool when respawned?

Asked by
TechModel 118
3 years ago
Edited 3 years ago

I want this script to drop the tool and remove it from its backpack, specifically when it is equipped. This script is in working order, but despite how I want it to really work, It doesn't. The issue I'm getting is that when I do die, the tool doesn't drop yet, until i respawn and it drops the tool I was holding. I want to make it when if you die, it immediately drops the tool so the enemy can pick it up and keep it, instead of waiting for 10 seconds for the body to despawn.

For those who wanted to know where this script is located is a script in StarterCharacterScripts.

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local backpack = player:WaitForChild("Backpack")

local hadToolEquipped = false

humanoid.Died:Connect(function()
    print("Humanoid Died")
    for i, tool in pairs(character:GetChildren()) do
        if tool:IsA("Tool") then
            hadToolEquipped = true
            tool.Parent = game.ReplicatedStorage
        end
    end
end)

player.CharacterRemoving:Connect(function(character)
    print("Character Removed")
    for i, tool in pairs(backpack:GetChildren()) do
        if tool:IsA("Tool") and tool:FindFirstChild("Handle") then
            tool.Parent = game.Workspace
            tool.Handle.CFrame = character.HumanoidRootPart.CFrame
        end
    end
    if hadToolEquipped == true then
        local equippedTool = game.ReplicatedStorage:FindFirstChildOfClass("Tool")
        equippedTool.Parent = game.Workspace
        equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
    end
end)

Answer this question