How to make UI disappear when tool is destroyed

So I’m trying to make a grenade UI disappear when you throw it and I’m using a remote event to try to make the UI go away but I don’t know how to do it, and the way I’m doing it, it doesn’t work.

Server script:

local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local DestroyedRemote = Tool:WaitForChild("Destroyed")
local Handle = Tool:WaitForChild("Handle")
local DamageScript = script:WaitForChild("Damage")
local Config = Tool:WaitForChild("Config")
local Heartbeat = game:GetService("RunService").Heartbeat

local LeftDown = false

local AttackAble = true
local AttackVelocity = Config.Velocity.Value

local Character = nil
local Humanoid = nil

--returns the wielding player of this tool
function getPlayer()
	local char = Tool.Parent
	return game:GetService("Players"):GetPlayerFromCharacter(Character)
end

function Toss(direction)
	local handlePos = Vector3.new(Tool.Handle.Position.X, 0, Tool.Handle.Position.Z)
	local spawnPos = Character.Head.Position
	spawnPos  = spawnPos + (direction * 5)
	Tool.Handle.Transparency = 1
	local Object = Tool.Handle:Clone()
	Object.Parent = workspace
	Object.Transparency = 1
	Object.Swing.Pitch = math.random(90, 110)/100
	Object.Swing:Play()
	Object.CanCollide = true
	Object.CFrame = Tool.Handle.CFrame
	Object.Velocity = (direction*AttackVelocity) + Vector3.new(0,AttackVelocity/7.5,0)
	Object.Trail.Enabled = true
	--Object.Fuse:Play()
	--Object.Sparks.Enabled = true
	local rand = 11.25
	Object.RotVelocity = Vector3.new(math.random(-rand,rand),math.random(-rand,rand),math.random(-rand,rand))
	Object:SetNetworkOwner(getPlayer())
	local ScriptClone = DamageScript:Clone()
	ScriptClone.Parent = Object
	ScriptClone.Disabled = false
	local tag = Instance.new("ObjectValue")
	tag.Value = getPlayer()
	tag.Name = "creator"
	tag.Parent = Object
	DestroyedRemote:FireClient(Character)
	Tool:Destroy()
end


script.Parent.Power.OnServerEvent:Connect(function(player, Power)
	AttackVelocity = Power
end)

Remote.OnServerEvent:Connect(function(player, mousePosition)
	if not AttackAble then return end
	AttackAble = false
	if Humanoid and Humanoid.RigType == Enum.HumanoidRigType.R15 then
		Remote:FireClient(getPlayer(), "PlayAnimation", "Animation")
	end
	local targetPos = mousePosition.p
	local lookAt = (targetPos - Character.Head.Position).unit
	Toss(lookAt)
	LeftDown = true
end)

function onLeftUp()
	LeftDown = false
end

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
end)

Tool.Unequipped:Connect(function()
	Character = nil
	Humanoid = nil
end)

Client script:

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
--local GUI = Player.PlayerGui
local PlayerUI = Player.PlayerGui
local UI = script:WaitForChild("ScreenGui") -- Your Gui Name Here
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local DestroyedRemote = Tool:WaitForChild("Destroyed")
local Tracks = {}
local InputType = Enum.UserInputType
local IsEquipped = false
local BeganConnection, EndedConnectionl
local ThrowBeganConnection, ThrowEndedConnectionl
local Power = script.Parent.Config.Velocity.Value


function playAnimation(animName)
	if Tracks[animName] then
		Tracks[animName]:Play()
	else
		local anim = Tool:FindFirstChild(animName)
		if anim and Tool.Parent and Tool.Parent:FindFirstChild("Humanoid") then
			Tracks[animName] = Tool.Parent.Humanoid:LoadAnimation(anim)
			playAnimation(animName)
		end
	end
end

function stopAnimation(animName)
	if Tracks[animName] then
		Tracks[animName]:Stop()
	end
end

function inputBegan(input)
	if input.UserInputType == InputType.MouseButton1 then
		if Power == 200 then

		playAnimation("High")
		wait(1.15)
		local lp = game.Players.LocalPlayer
		local ms = lp:GetMouse()
		if not IsEquipped then return end
		Remote:FireServer(ms.Hit)

		elseif Power == 150 then
			
		playAnimation("Med")
		wait(1.15)
		local lp = game.Players.LocalPlayer
		local ms = lp:GetMouse()
		if not IsEquipped then return end
		Remote:FireServer(ms.Hit)

		elseif Power == 75 then
			
		playAnimation("Low")
		wait(1.15)
		local lp = game.Players.LocalPlayer
		local ms = lp:GetMouse()
		if not IsEquipped then return end
		Remote:FireServer(ms.Hit)

		end
	end
end


function ThrowType(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F then
			print("Low Throw:",input.KeyCode)
			Power = 75
			script.Parent.Power:FireServer(Power)
		elseif input.KeyCode == Enum.KeyCode.G then
			print("Med Throw:",input.KeyCode)
			Power = 150
			script.Parent.Power:FireServer(Power)
		elseif input.KeyCode == Enum.KeyCode.H then
			print("High Throw:",input.KeyCode)
			Power = 200
			script.Parent.Power:FireServer(Power)
		end
	end
end

function onEquip()
	local GrenadeUI = UI:Clone()
	GrenadeUI.Parent = game.Players.LocalPlayer.PlayerGui
	--GUI.ScreenGui.Enabled = true
	BeganConnection = UIS.InputBegan:connect(inputBegan)
	ThrowBeganConnection = UIS.InputBegan:Connect(ThrowType)
	IsEquipped = true
end

function onUnequip()
	PlayerUI:FindFirstChild(UI.Name):Destroy()
	--GUI.ScreenGui.Enabled = false
	if BeganConnection then
		BeganConnection:disconnect()
		ThrowBeganConnection:disconnect()
		BeganConnection = nil
		ThrowBeganConnection = nil
		IsEquipped = false
	end
end

DestroyedRemote.OnClientEvent:Connect(function()
	PlayerUI:FindFirstChild(UI.Name):Destroy()
end)

Tool.Equipped:connect(onEquip)
Tool.Unequipped:connect(onUnequip)
Tool.Destroying:Connect(function()
  -- remove UI
end)

It just didn’t work

qqqqqqqqqq

I fixed it by doing Tool.Activated instead of Tool.Destroying.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.