FindFirstChild not working

Hello, Im working on a dragable gui system and I am getting the selected slot by using

local selectedSlot = frame.Slot1
for _,v in pairs(frame:GetChildren()) do
	v.MouseEnter:Connect(function()
		selectedSlot = v
	end)
end

and I am using InputBegan to detect if there is input.

selectedSlot.InputBegan:Connect(function(input) --can't be DragableGui
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
		print("e")
		local y = InvFrame:FindFirstChild(CurrInvFrame) --issue occurs here, prints nil
		local p = y:FindFirstChild(selectedSlot) --issue occurs here, might print nil here
		DragableGui = p.Position

		dragToggle = true
		dragstart = input.Position
		startPos = DragableGui.Position
		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				selectedSlot = nil
				dragToggle = false
			end
		end)
	end
end)

How can I fix this issue where FindFirstChild is not getting the item? If i print currInvFrame it prints the base which contains the slots and if i print selectedSlot then it prints the slot name.

image

1 Like

What is the InvFrame and what does it contain? Maybe try adding .Name to CurrInvFrame and selectedSlot

Also what is changing the contents of CurrInvFrame and SelectedSlot

What is CurrInvFrame? If you print CurrInvFrame, does it print nil?

When he prints CurrInvFrame, he mentioned that it prints the base where all the slots are contained, it’s probably an issue as to how he’s getting them, or something else

when i print CurrInvFrame it prints the name which is base

What is InvFrame? How are you switching CurrInvFrame and selectedSlot?

I added .Name to the end of CurrInvFrame and selectedSlot and it worked, I thought it didn’t because it is a variable not an actual object in the Gui. I am only comparing it to see if it exists

2 Likes

Oh, I honestly thought it wouldn’t work but now that I see your code it’s probably reasonable how that worked since you’re giving FindFirstChild an instance, not a string, so giving it the name is how you would do that

If you have anymore issues don’t be afraid to make another post!

How can I update the selectedSlot for selectedSlot.InputBegan:Connect(function(input) because Its currently a constant value which I assigned and the value is Slot1, and when the mouse moves over other gui’s it gets updated to that slot but I get an error saying attempt to index nil with 'Name' after I have dragged the Gui once.

What is your current code right now if I may ask? I think I may see something that could fix it

1 Like
local dragToggle = nil
local dragSpeed = -1
local dragstart = nil
local startPos = nil
local DragUIPos = nil
local selectedSlot = frame.Slot1

local function UpdateInput(input)
	local delta = input.Position-dragstart
	local pos = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
	game:GetService("TweenService"):Create(DragableGui, TweenInfo.new(dragSpeed), {Position = pos}):Play()
end

for _,v in pairs(frame:GetChildren()) do
	v.MouseEnter:Connect(function()
		selectedSlot = v
	end)
end

selectedSlot.InputBegan:Connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
		local y = InvFrame:FindFirstChild(CurrInvFrame.Name)
		local p = y:FindFirstChild(selectedSlot.Name)
		
		DragableGui.Position = p.Position
		DragableGui.Visible = true

		dragToggle = true
		dragstart = input.Position
		startPos = DragableGui.Position

		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				selectedSlot = nil
				dragToggle = false
			end
		end)
	end
end)

UIS.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if dragToggle then
			UpdateInput(input)
		end
	end
end)
1 Like

Try this?

local dragToggle = nil
local dragSpeed = -1
local dragstart = nil
local startPos = nil
local DragUIPos = nil
local selectedSlot = frame.Slot1

local connection1
local connection2 

local function UpdateInput(input)
	local delta = input.Position-dragstart
	local pos = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
	game:GetService("TweenService"):Create(DragableGui, TweenInfo.new(dragSpeed), {Position = pos}):Play()
end

for _,v in pairs(frame:GetChildren()) do
	v.MouseEnter:Connect(function()
		selectedSlot = v
		if connection1 or connection2 then return end
		
		connection1 = selectedSlot.InputBegan:Connect(function(input)
			if (input.UserInputType == Enum.UserInputType.MouseButton1) then
				local y = InvFrame:FindFirstChild(CurrInvFrame.Name)
				local p = y:FindFirstChild(selectedSlot.Name)
				
				DragableGui.Position = p.Position
				DragableGui.Visible = true

				dragToggle = true
				dragstart = input.Position
				startPos = DragableGui.Position
			end
		end)
		
		connection2 = selectedSlot.InputEnded:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				selectedSlot = nil
				dragToggle = false
				
				if connection1 then connection1:Disconnect() end
				if connection2 then connection2:Disconnect() end
			end
		end)
	end)
end

UIS.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if dragToggle then
			UpdateInput(input)
		end
	end
end)

It’ll put them into their own connections in MouseEnter and if there’s alreayd connections are not disconnected by releasing left click, then it wont do anything. I think it should work, but I’m unsure

this only worked once, any idea on how to fix it?

Maybe remove that line from my code?

1 Like

this fixed the issue with it only working once, but how can I fix the errors if i just click? I get an error saying attempt to index nil with ‘Name’ and it happens at local p = y:FindFirstChild(selectedSlot.Name)

Try adding a condition before that line

if not selectedSlot then return end

So if it’s nil, it wont try to get the name

if not selectedSlot then return end
local y = InvFrame:FindFirstChild(CurrInvFrame.Name)
local p = y:FindFirstChild(selectedSlot.Name)
1 Like

thanks so much for your help with these issues

1 Like

Anytime! If you hae anymoer issues don’t be afraid to make another post! Although I’m unsure if the way I did it is the best way to go about it, maybe just try using v instead of selectedSlot, although that may cause other issues

If you have anymore issues don’t be afraid to make another post!