Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my tool not deleting from starter gear?

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

✓ Accepted answer

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

2 comments

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?

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.