Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Rheines 661 Moderation Voter
6 years ago
Edited 6 years ago

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?

0
You're calling the function before you define it on Line 9. xPolarium 1388 — 6y
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. SummerEquinox 643 — 6y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 5 years ago

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.
0
Simple mistake, thank you so much! Learned something new. Rheines 661 — 6y
Ad

Answer this question