Why isn't this working?

I made a script that lets the player mine blocks, but I’m having an issue with showing up the progress bar and the rest of the GUI. Basically, I intentionally left out a GUI Folder so that one can be made, but the script still will give me the same error that GUI does not exist even though I put an elseif not GUI.

  12:56:07.950  GUI is not a valid member of Part "Workspace.Red_Mineral"  -  Server - 

Here is the script in question, and I have marked the part that has the elseif not in it.

local tool = script.Parent
local toolDamage = tool:GetAttribute("Damage")
local guiElements = tool:WaitForChild("GUI")
local remote = tool:WaitForChild("RemoteEvent")

remote.OnServerEvent:Connect(function(player, char, mineral)
	toolDamage = tool:GetAttribute("Damage")
	if mineral.GUI and mineral:GetAttribute("Health") then
		local maxHealth = mineral:GetAttribute("MaxHealth")
		local health = mineral:GetAttribute("Health")
		mineral:SetAttribute("Health", health - toolDamage)
		mineral.GUI:WaitForChild("ProgressBar").Size = UDim2.new(health/maxHealth, 0, 0, 0)
	elseif not mineral.GUI and mineral:GetAttribute("Health") then -- <--
		for i, v in pairs(guiElements:GetChildren()) do
			local guiClone = v:Clone()
			guiClone.Parent = mineral
			for ii, vv in pairs(guiClone:GetChildren()) do
				vv.BackgroundTransparency = 0
				vv.TextTransparency = 0


Well the error is self-explanatory, GUI doesnt exist in “red_minerals”

But the thing is, I wrote elseif not the child doesn’t exist and the health attribute exist, but it never goes to the elseif.

I intentionally made it so that the GUI wasn’t avaliable so that I can insert a new GUI folder into it, as seen after the elseif text.

Use :FindFirstChild("GUI") instead of indexing it right away, because indexing it right away and it doesnt exist, luau wont like that and it will throw an error, whereas findfirstchild checks if it exists, and if it does, it returns the object, but if it doesnt, it returns nil without throwing an error

1 Like

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