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

Why is my tool not deleting from starter gear?

Asked by
ruwuhi 23
4 years ago

I am trying to make the tool in a user's hand delete from the character and also from starter gear. However, it only deletes from the character and not from starter gear. Below is inside a local script under an image button. Why is this happening?


local function onCreatePartFired(player, tool) local SG = player.StarterGear if tool then tool:Destroy() end if SG:FindFirstChild(tool.Name) then print("hey") SG[tool.Name]:Destroy() end end local Players = game:GetService("Players") plr = game:GetService("Players").LocalPlayer char = plr.Character or plr.CharacterAdded:Wait() script.Parent.MouseButton1Click:Connect(function() local tool = char:FindFirstChildOfClass("Tool") onCreatePartFired(plr,tool) end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

If "tool" exists, then the second if block will never run because you'll destroy the tool before being able to access its name. Try:

local function onCreatePartFired(player, tool)
    local SG = player.StarterGear
    if tool then
        local sg_tool = SG:FindFirstChild(tool.Name)
        if sg_tool then
            print("hey")
            sg_tool:Destroy()
        end
        tool:Destroy()
    end
end
0
Hello! Thank you, it's working! But, for some reason when I respawn, the item comes back to backpack even though I deleted it from starter gear and the character. Is there any reason for that? ruwuhi 23 — 4y
0
This sounds like it's because you're deleting the tool from the current instance of the player, and when the player respawns the tool exists wherever it's loading the new character from. Similar to how when you start a game, anything in StarterGui moves to PlayerGui, and if you delete something from the StarterGui in game, the player's GUI will not be updated. Does that make sense? Hycheese 34 — 4y
Ad

Answer this question