Knit not allowing access to Gui

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to access Gui from the controllers in knit but it’s bugging out on me

  2. What is the issue? so basically I have tried to access gui by referencing StarterGui, it doesn’t work so I found out you had to reference PlayerGui. I did that but a bug keeps on occurring on KnitClient saying that it “experienced an error while loading”

  3. What solutions have you tried so far? I changed everything I could using :WaitForChild and everything. When it didn’t bug the Gui just wouldn’t respond entirely.

Is there any way to fix this because I’ve spent half an hour trying to figure out what’s going on but nothing is happening, It’s getting quite annoying.

local player = game.Players.LocalPlayer

local GuiController = knit.CreateController {
	Name = "GuiController",
}

local PlayerGui = player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui

This is my code. I am using Knit framework. I think it’s the last line that is bugging out.

Maybe it’s me, but I don’t see you requiring Knit anywhere, also I recommend referencing PlayerGUI in your local script that starts Knit on the client, like that :

-- in the client script that starts knit

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

local Knit = require(PATH.TO.KNIT)

local Player : Player = Players.LocalPlayer
local PlayerGui : PlayerGui = Player.PlayerGui
local Mouse : Mouse = Player:GetMouse()

local Client = {}
Client.__index = Client
Client.KnitStart = os.clock()

--[=[
@table Knit._CS
@brief A table that holds references to various services and objects.
]=]
Knit._CS = {
    ["Player"] = Player,
    ["PlayerGui"] = PlayerGui,
    ["Mouse"] = Mouse,
    -- u can add more
}

--[=[
@function AddControllersDeep
@brief Adds all the controllers from the Controllers folder to the Knit framework.
]=]
Knit.AddControllersDeep(ReplicatedStorage.Shared:WaitForChild("Controllers") :: Folder)

--[=[
@function Start
@brief Starts the Knit framework with the provided server configuration.
@details This function initializes the Knit framework on the client side. It adds all the controllers from the ControllersFolder to the Knit framework, and then starts the framework. If the start is successful, it prints a message to the console. If there's an error during the start, it catches the error and prints an error message to the console.
@parameter table options A table of options for starting the Knit framework. The `ServicePromises` field is set to `false` in this case.
@return Promise A Promise that resolves when the Knit framework has started successfully, or rejects if there's an error.
]=]
Knit.Start({ServicePromises = false}):andThen(function()
    warn(string.format("[Client] Knit started at %sms", (os.clock() - Client.KnitStart)))
end):catch(function(error)
    warn("[Server] Failed to start Knit: " .. tostring(error))
end):await()

Then in the controller you can reference player gui like this :

	local Interface : PlayerGui= Knit._CS.PlayerGui

My code is in the module script. I only provided a small snippet but I did reference knit on the top. I’m still a bit of a beginner so please could you explain the code? Thanks.

First off, I don’t think a beginner should start using Knit, but sure I’ll explain how this works

Knit allows you to pass tables through Knit.[yourTable] = {}

and thats basically what we’re doing, we made a .CS table and we pass it through Knit, that allows us to access it on EVERY controller (not services this is client sided), in this example, in the table I pass Player & PlayerGui, that means you should be able to access it through a controller using this :

local PlayerGUI = Knit._CS.PlayerGui
-- in a controller btw

I can code a game with normal scripts. I’m just new to OOP and SSA and module scripts. I ran your code but I’ve still got the same error from my old code with yours. I’m starting to feel it’s a problem with knit

Can you give me your full controller code? or a snippet where it errors

this is in a module script.

local RepStorage = game:GetService("ReplicatedStorage")
local knit = require(RepStorage.Packages.Knit)

local GuiController = knit.CreateController {
	Name = "GuiController",
}

local Interface : PlayerGui= knit._CS.PlayerGui

local Frame = Interface.Frame
local EditButton = Interface.TextButton
local DisapearFrame = Frame.DisapearFrame
local ArrowFrame = Frame.ArrowFrame
local MoveFrame = Frame.MoveFrame
local DisapearButton = Frame.Disapear
local MoveButton = Frame.Move

function GuiController.EditButton()
	EditButton.MouseButton1Click:Connect(function()
		Frame.Visible = not Frame.Visible
	end)
end

function GuiController:KnitInit()
	print("Guiservice initialised")
end

function GuiController:KnitStart()
	print("Guiservice started")
end

return GuiController

you can see I put your refernce in the middle

Try this :

local RepStorage = game:GetService(“ReplicatedStorage”)
local knit = require(RepStorage.Packages.Knit)

local GuiController = knit.CreateController {
Name = “GuiController”,
}

function GuiController.EditButton()
   local Frame = knit._CS.PlayerGui.Frame
   EditButton.MouseButton1Click:Connect(function()
       Frame.Visible = not Frame.Visible
   end)
end

function GuiController:KnitInit()
	local Interface : PlayerGui= knit._CS.PlayerGui

	local Frame = Interface.Frame
	local EditButton = Interface.TextButton
	local DisapearFrame = Frame.DisapearFrame
	local ArrowFrame = Frame.ArrowFrame
	local MoveFrame = Frame.MoveFrame
	local DisapearButton = Frame.Disapear
	local MoveButton = Frame.Move
end

function GuiController:KnitStart()
print(“Guiservice started”)
end

return GuiController

The error is probably caused because you try to reference .Frame before it even got created, thats why I put it in KnitInit()

It used your warn from your last code.
the error was on the knit module again I think.
It also said Frame is not a valid member of PlayerGui

Yeah thats what I said, you’re trying to find Frame when it doesn’t even exist (because the game didn’t load yet), thats why you should put it in either KnitStart or KnitInit

I did put everything in KnitInit.
It’s the same error.
Could it be a problem with knit? everything doesn’t seem to work even with your help.

Mmh try putting it in KnitStart

Put a WaitForChild on it. Now it doesn’t bug, instead it says Infinite yield possible. Plus my Gui still doesn’t respond to my clicks.

It’s simple, you either put it in KnitStart or you use WaitForChild(“Frame”,999)

everything after my :WaitForChild just refuses to work now

Nvm somehow something worked and the scripts fine now

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