Hello everyone. So this isn’t exactly a problem but instead, something I haven’t found anywhere on the internet but on ChatGPT with a few prompts and I’d like to help people save their time.
Usually when you want to send a piece of information from the server to the client or vice-versa, you’d do something like:
--Side: Server.
local newSignal = Service.Client.CreateSignal();
newSignal:Fire(player, newInfo);
And then:
--Side: Client
Service = Knit.GetService();
Service.newSignal:Connect(function(newInfo)
print("Got new info:", newInfo);
end);
Cool. But what if I want to similarly warn all my controllers (client side) that something on the client side happened that is interesting to many controllers? If I wasn’t clear enough, I’ll give you an example.
I was trying to warn all my controllers (UIController, AimController, etc…) that the client connected a gamepad (device switch). That way, UIController could instantly change icons accordingly to the new device type.
The solution is: We, naturally, have a InputController that listens to UserInputService.GamepadConnected/Disconnected. That exact same controller will create the signal and fire it when needed.
--Side: Client
function InputController:KnitStart()
local function warnEveryone()
--???
end;
UserInputService.GamepadConnected:Connect(warnEveryone)
UserInputService.GamepadDisconnected:Connect(warnEveryone)
end;
We can’t use Knit.CreateSignal() here, I’ve tried and it says that we tried to call a nil value.
The solution is to go straight to the Packages.Signal module and use the ‘new’ function to manually create ourselves a signal like so:
--Side: Client
local SignalModule = require(ReplicatedStorage.Packages.Signal);
function InputController:KnitStart()
self.newSignal = SignalModule.new();
local function warnEveryone()
self.newSignal:Fire(newDevice);--This will warn everyone that used InputController.newSignal:Connect(function)
end;
UserInputService.GamepadConnected:Connect(warnEveryone)
UserInputService.GamepadDisconnected:Connect(warnEveryone)
end;
Hope this helps.