Tool interacting with a viewport frame after its destroyed

Alright so I have a big issue with tools and viewport frames, Whenever I click the button to choose a tool, It shows the tool in a viewport frame, And the ammo GUI Pops up on the players screen for some reason, and thats only suppose to happen with a :Equipped() function, Once I purchase the tool I close the GUI with the viewport frame by destroying it, and the weapon gets cloned seperately, and isnt the one used in the viewport frame, Now for some reason whenever the player activates the tool, I get this error

Players.CQuaral.PlayerGui.ScreenGui.ViewportFrame.Glock.LocalScript:37: attempt to index nil with 'Parent' 

The line with the error is just checking if theres a humanoid where the mouse target is, But the issue is that for some reason it still is with the viewport frame, I have no idea whats happening which is why im making this post

Can you show use the actual code, we cant help with no code provided

This is the LocalScript in the tool

local Active = false
local Equipped = false

script.Parent.Equipped:Connect(function()
	Equipped = true
	local GUI = script.Parent.ScreenGui:Clone()
	GUI.Parent = game.Players[script.Parent.Parent.Name].PlayerGui
	
	GUI.TextLabel.Text = script.Parent.Ammo.Value.." / "..script.Parent.MaxAmmo.Value

	script.Parent.Ammo:GetPropertyChangedSignal("Value"):Connect(function()
		GUI.TextLabel.Text = script.Parent.Ammo.Value.." / "..script.Parent.MaxAmmo.Value
	end)
	
	game.Players.LocalPlayer:GetMouse().Icon = "rbxassetid://10512637573"
	local Anim = Instance.new("Animation")
	Anim.AnimationId = "rbxassetid://10512699870"
	local loadanim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Anim)
	loadanim:Play()
	
	script.Parent.Unequipped:Connect(function()
		GUI:Destroy()
		Equipped = false
		game.Players.LocalPlayer:GetMouse().Icon = [[]]
		loadanim:Stop()
			Anim:Destroy()
		end)
end)

script.Parent.Activated:Connect(function()
	print(script.Parent.Parent.Name)
	if Active then return end

	Active = true
	local target = game.Players.LocalPlayer:GetMouse().Target

	if target.Parent:FindFirstChild("Humanoid") then
		if (target.Parent.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude < 60 then
			if script.Parent.Ammo.Value > 0 then
				script.Parent.RemoteEvent:FireServer()
			game.Players.LocalPlayer:GetMouse().Icon = "rbxassetid://10512652996"
			task.wait(.2)
			game.Players.LocalPlayer:GetMouse().Icon = "rbxassetid://10512637573"
			end
			end
	end
	task.wait(.2)
	Active = false
end)

This is the module script that controls purchasing, Its called by a server script

local WeaponPurchase = {}
function WeaponPurchase.PurchaseMenu(player)

	local WeaponClient = require(game.ReplicatedStorage.Client.WeaponsCost)
	local Selected = nil
	local ScreenGui = Instance.new("ScreenGui")
	local ViewportFrame = Instance.new("ViewportFrame")
	local ScrollingFrame = Instance.new("ScrollingFrame")
	local UIGridLayout = Instance.new("UIGridLayout")
	local Button = Instance.new("TextButton")
	local UICorner = Instance.new("UICorner")
	local UIStroke = Instance.new("UIStroke")
	local ExitButton = Instance.new("TextButton")
	local ExitCorner = Instance.new("UICorner")
	local ExitStroke = Instance.new("UIStroke")
	local NewCamera = Instance.new("Camera")
	
	ScreenGui.Enabled = true
	ViewportFrame.Parent = ScreenGui
	ScrollingFrame.Parent = ScreenGui
	UIGridLayout.Parent = ScrollingFrame
	ScreenGui.Parent = player.PlayerGui
	Button.Parent = ScreenGui
	UICorner.Parent = Button
	UIStroke.Parent = Button
	
	ExitButton.Parent = ScreenGui
	ExitButton.BackgroundColor3 = Color3.new(0.666667, 0, 0)
	ExitButton.Position = UDim2.new(0.681, 0,0.782, 0)
	ExitButton.Size = UDim2.new(0.04, 0,0.064, 0)
	ExitButton.Font = Enum.Font.Nunito
	ExitButton.Text = "X"
	ExitButton.TextScaled = true
	ExitButton.TextColor3 = Color3.new(1, 1, 1)
	
	ExitCorner.Parent = ExitButton
	ExitCorner.CornerRadius = UDim.new(0, 18)
	
	ExitStroke.Parent = ExitButton
	ExitStroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
	ExitStroke.Color = Color3.new(0.427451, 0, 0)
	ExitStroke.Thickness = 4
	
	ViewportFrame.Transparency = 0.8
	ViewportFrame.Size = UDim2.new(0.4, 0,0.3, 0)
	ViewportFrame.BackgroundColor3 = Color3.new(0, 0, 0)
	ViewportFrame.Ambient = Color3.new(1, 1, 1)
	ViewportFrame.LightColor = Color3.new(1, 1, 1)
	ViewportFrame.BorderSizePixel = 0
	ViewportFrame.Position = UDim2.new(0.3, 0,0.519, 0)
	
	UIGridLayout.CellSize = UDim2.new(0.95, 0,0.03, 0)
	
	ScrollingFrame.BackgroundColor3 = Color3.new(0.117647, 0.117647, 0.117647)
	ScrollingFrame.BorderSizePixel = 2
	ScrollingFrame.BorderColor3 = Color3.new(1, 1, 1)
	ScrollingFrame.Size = UDim2.new(0.4, 0,0.5, 0)
	ScrollingFrame.ScrollBarImageColor3 = Color3.new(1, 1, 1)
	ScrollingFrame.Position = UDim2.new(0.3, 0,0.02, 0)
	
	Button.BackgroundColor3 = Color3.new(0.258824, 0.780392, 0)
	Button.Font = Enum.Font.Nunito
	Button.TextScaled = true
	Button.Size = UDim2.new(0.2, 0,0.1, 0)
	Button.Text = "Purchase"
	Button.Position = UDim2.new(0.39, 0,0.766, 0)
	Button.TextColor3 = Color3.new(1, 1, 1)
	
	UICorner.CornerRadius = UDim.new(0, 18)
	UIStroke.Thickness = 5
	UIStroke.Color = Color3.new(0.176471, 0.533333, 0)
	NewCamera.Parent = ViewportFrame
	
	ExitButton.MouseButton1Click:Connect(function()
		ScreenGui:Destroy()
	end)
	
	Button.MouseButton1Click:Connect(function()
		if Selected then
			
			local Cost = require(game.ReplicatedStorage.Client.WeaponsCost)[Selected].Cost
			
			if player.Wallet.Value >= Cost then
				player.Wallet.Value = player.Wallet.Value - Cost
				local TheHun = game.ServerStorage.Weapons[Selected]:Clone()
				local NotificationHandler = require(game.ReplicatedStorage.Client.ClientModules)
				
				TheHun.Parent = player.Backpack
				NotificationHandler.notification(player, "You've purchased "..TheHun.Name)
			end
		else
			print("No weapon selected")
		end
	end)
	
	for Client, Weapons in pairs(WeaponClient) do
		
		local TextButton = Instance.new("TextButton")
		local StringValue = Instance.new("StringValue")
		local UICorner = Instance.new('UICorner')
		local UIStroke = Instance.new("UIStroke")
		
		StringValue.Parent = TextButton
		StringValue.Value = Weapons.Name
		
		TextButton.BackgroundColor3 = Color3.new(0, 0.333333, 0.498039)
		TextButton.Text = Weapons.Name.." - $"..Weapons.Cost
		TextButton.Font = Enum.Font.Highway
		TextButton.TextScaled = true
		TextButton.Parent = ScrollingFrame
		
		UICorner.CornerRadius = UDim.new(0,18)
		UICorner.Parent = TextButton
		
		UIStroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
		UIStroke.Thickness = 1
		UIStroke.Color = Color3.new(1, 1, 1)
		UIStroke.Parent = TextButton
		
		TextButton.MouseButton1Click:Connect(function()
			Selected = StringValue.Value
			for yes, no in pairs(ViewportFrame:GetChildren()) do
				if no:IsA("Tool") then
					no:Destroy()
				end
			end
			local Weapon = game.ServerStorage.Weapons[StringValue.Value]:Clone()
			local Handle = Weapon.Handle
			
			Weapon.Parent = ViewportFrame
			
			ViewportFrame.CurrentCamera = NewCamera
			NewCamera.CFrame = CFrame.new(Handle.Position + (Handle.CFrame.LookVector * 1) + Vector3.new(0,1,0) + (Handle.CFrame.RightVector * 1), Handle.Position)
			
		end)
		
	end
	
end
return WeaponPurchase
1 Like

Are you sure the gui isn’t being enabled when cloning (by another script) or you had it earlier and it got enabled?

Yup im sure I just made the GUI And local script and theres no other scripts there that are making or cloning GUI’s

1 Like

You can detect when the parent changes

tool:GetPropertyChangedSignal("Parent"):Connect(function()
	if tool.Parent:IsA("BackPack") then --check the IsA might have miss spelled it
		GUI:destroy()
	end
end)

And change some stuff so it works