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

Attempt to index nil with 'destroy' | Trying to destroy a GUI | (?)

I followed a youtube tutorial, and I know the very basics of scripting but I'm gradually learning. When attempting to run the last part of the script, I get the error "ServerScriptService.OverheadHandler:17:attempt to index nil with 'destroy'" Can anyone help?

local serverStorage = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local level = plr:WaitForChild("leaderstats").Rank.Level
		local levelVal = level.Value
		local rankTag = serverStorage:FindFirstChild(levelVal):Clone()
		rankTag.Parent = char.Head
		plr.leaderstats.Rank.Level.Changed:Connect(function()
			local level = plr:WaitForChild("leaderstats").Rank.Level
			local levelVal = level.Value
			local rankTag = serverStorage:FindFirstChild(levelVal):Clone()
			rankTag.Parent = char.Head
			local lastRank = levelVal - 1
			
			if lastRank > 0 then
				char.Head:FindFirstChild(lastRank):Destroy()
			end
		end)
	end)
end)

1 answer

✓ Accepted answer

You cant destroy GUI u can only make it unvisible

GUI = char.Head:FindFirstChild(lastRank)
GUI.Visible = False

2 comments

0

I did that, it doesn't do it either, the issue seems to be that the GUI is parented to the player, and not their head.

0

Wait, nevermind, I figured out how to make your code work. Thanks!

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