How to add line for when the tool is dropped?

I have this script that turns on the GUI inside of the tool. Whenever the player drops the tool, the GUI doesn’t destroy itself. Is there a line of code that I can add that detects when the tool is dropped and that means I can turn off or destroy the GUI

script.Parent.Equipped:Connect(function()
	local playerName = script.Parent.Parent.Name
	local player = game.Players:FindFirstChild(playerName)
	local gui = script.Parent.HUD
	gui.Parent = player.PlayerGui
end)

script.Parent.Unequipped:Connect(function()
	local player = script.Parent.Parent.Parent
	local gui = player.PlayerGui.HUD
	gui.Parent = script.Parent
end)
1 Like
workspace.ChildAdded:Connect(function(item)
    if item:IsA("Tool") then
        if not item.Parent:IsA("Model") and game.Players:GetPlayerFromCharacter(item.Parent) then
            -- Do something fun!
        end
    end
end

Please mark this as a solution if it helps, thanks.

it doesn’t seem to be doing anything…

Put it in a Script in ServerScriptService

how would I access the player and their PlayerGui from there then?

Try adding this to reference the player

game.Players:GetPlayerFromCharacter(item.Parent)

what does that do exactly? When the players drop the tools, the hud from other tools layers on top as well, so I don’t know how this will help

local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local character = player.Character or player.CharacterAdded:Wait()

local tool = script.Parent
local hud = tool:WaitForChild("HUD")

tool.Equipped:Connect(function()
	hud.Parent = playerGui
end)

tool.Unequipped:Connect(function()
	hud.Parent = tool
end)

character.ChildRemoved:Connect(function(child)
	if child == tool then
		hud.Parent = tool
	end
end)

Assuming this is a local script.

You can also disable the tool’s CanBeDropped property.

https://developer.roblox.com/en-us/api-reference/property/Tool/CanBeDropped

2 Likes

oh my god thank you so much man, it worked

My bad, just realizing I didn’t read this right lol