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

Function calling help, what can I do to circumvent this error?

Hello, I've been trying to create an inventory system where it will redraw GUI every time the descendants of the inventory folder has been changed. I created a function to do this in a LocalScript called RedrawGrids(), but when I tried to call it using a MouseEnter I receive the following error in the output:

06:02:49.260 - Players.Rheines.PlayerGui.NewInventory.MainFrame.Inventory.Padding.LocalScript:9: attempt to call global 'RedrawGrids' (a nil value)

This is my script:

local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = PlayerService.LocalPlayer
local Equipped = Player:WaitForChild("Equipped")
local Inventory = Player:WaitForChild("Inventory")

--Currently use MouseEnter to check if it works.
script.Parent.MouseEnter:Connect(RedrawGrids())

--Redraw inventory grids.
function RedrawGrids()
	print('entered function')
	--Clear all gui to redraw with updated inventory.
	for _,guis in pairs(script.Parent:GetChildren()) do
		if guis:IsA("ImageLabel") then
			guis:Destroy()	
		end
	end
	--Redraw inventory contents.
	for _,gear in pairs(Inventory:GetChildren()) do
		local gui = ReplicatedStorage.ItemGUI:Clone()
		gui.Name = gear.Type.Value
		gui.Parent = script.Parent
	end
end

However, when I made RedrawGrids into an anonymous function such as

script.Parent.MouseEnter:Connect(function()
	print('entered function')
	--Clear all gui to redraw with updated inventory.
	for _,guis in pairs(script.Parent:GetChildren()) do
		if guis:IsA("ImageLabel") then
			guis:Destroy()	
		end
	end
	--Redraw inventory contents.
	for _,gear in pairs(Inventory:GetChildren()) do
		local gui = ReplicatedStorage.ItemGUI:Clone()
		gui.Name = gear.Type.Value
		gui.Parent = script.Parent
	end
end)

it works perfectly fine. However, I want to call RedrawGrids as a variable so that I can reuse it as often as I like. Is there something that I did wrong?

2 comments

0

You're calling the function before you define it on Line 9.

0

The issue is that in your first attempt - function RedrawGrids doesn't exist when you try to use it. In your second attempt - it works because you are hooking MouseEnter directly to the events you want to happen. To circumvent this, create the RedrawGrids function before you try to hook it to an event.

1 answer

✓ Accepted answer

The error is: in :Connect do not use :Connect(function()) you need to use :Connect(function) and you need to put it after function to detect that it is not a nil value

Here is fixed script:

local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = PlayerService.LocalPlayer
local Equipped = Player:WaitForChild("Equipped")
local Inventory = Player:WaitForChild("Inventory")

-- Changed location of MouseEnter.

--Redraw inventory grids.
function RedrawGrids()
    print('entered function')
    --Clear all gui to redraw with updated inventory.
    for _,guis in pairs(script.Parent:GetChildren()) do
        if guis:IsA("ImageLabel") then
            guis:Destroy()  
        end
    end
    --Redraw inventory contents.
    for _,gear in pairs(Inventory:GetChildren()) do
        local gui = ReplicatedStorage.ItemGUI:Clone()
        gui.Name = gear.Type.Value
        gui.Parent = script.Parent
    end
end

--Currently use MouseEnter to check if it works.
script.Parent.MouseEnter:Connect(RedrawGrids) -- removed "()" after "RedrawGrids"

Hope it helped :D

Errors? tell-me on comments.

Solved your problems? put in title [SOLVED] or accept a answer.

1 comment

0

Simple mistake, thank you so much! Learned something new.

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