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

Local script can't find descendants in workspace?

I have a local script in a ScreenGui which is meant to get all descendants with the name ClickDetector and if the mouse hovers enters/leaves or clicks it changes the size of some images but I don't think it can find any descendants in the workspace when there are some, just get the error attempt to index nil with connect or function.

local ClickDetector = game.Workspace:GetDescendants("ClickDetector")
local Ring = script.Parent:FindFirstChild("Ring")

ClickDetector.MouseHoverEnter:Connect(function()
	Ring.Size = UDim2.new(0, 20, 0, 20)
end)

ClickDetector.MouseHoverLeave:Connect(function()
	Ring.Size = UDim2.new(0, 15, 0, 15)
end)

ClickDetector.MouseClick:Connect(function()
	Ring.Size = UDim2.new(0, 10, 0, 10)
	wait(0.5)
	Ring.Size = UDim2.new(0, 15, 0, 15)
end)

Could someone point out what I've done wrong? Thanks!

2 answers

✓ Accepted answer

That is not how you use GetDescendants

local Descendants = game.Workspace:GetDescendants()
local Ring = script.Parent:FindFirstChild("Ring")

for _,v in pairs (Descendants) do -- Looping through the descendants
	if v:IsA("ClickDetector") then -- Checking if the v also known as the value of the descendant class is a ClickDetector
		v.MouseHoverEnter:Connect(function() -- MouseEnter Function
    			Ring.Size = UDim2.new(0, 20, 0, 20)
		end)
		v.MouseHoverLeave:Connect(function() -- MouseLeaveFunction
   		 	Ring.Size = UDim2.new(0, 15, 0, 15)
		end)
		v..MouseClick:Connect(function() -- MouseClick Function
    			Ring.Size = UDim2.new(0, 10, 0, 10)
    			wait(0.5)
    			Ring.Size = UDim2.new(0, 15, 0, 15)
		end)
	end
end 

If your struggling to understand I will link the resources here IsA Here GetDescendants Here For Loop Here

Ah you see, GetDescendants() is a lot like GetChildren(), so although your code will get descendants, putting a string inside inside the brackets will not make it find the descendant you are looking for, so instead, you can make the variable get the descendants and another variable get the children you are looking for.

Here's an example:

local Descendants = game.Workspace:GetDescendants()

local ClickDetector = Descendants.ClickDetector

-- rest of your code here

Hope this helps!

Any questions? Just ask!

2 comments

0

So I think thats solved that part of it but now I'm getting the error attempt to index nil with 'MouseHoverEnter'

0

Oh ok ill try to fix that

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