I'm very new to scripting with this kind of stuff, and I don't know what else to do for this event.
Basically, I have a Local Script which connects to a Server script. I want the Server script to execute commands to other scripts in the bus, but I ran the Script Debug errors and one error came up as "attempt to index local 'Bus' (a nil value)"
Here's the scripts:
Local script:
local uiService = game:GetService("UserInputService") local event = game.ReplicatedStorage.BusEvent local mouse = game.Players.LocalPlayer:GetMouse() local Bus = game.Workspace:FindFirstChild(game.Players.LocalPlayer.PlayerGui.TGUI.Value) uiService.InputBegan:Connect(function(key) event:FireServer(key.KeyCode) end)
Server script:
local event = game.ReplicatedStorage.BusEvent event.OnServerEvent:Connect(function(player, key) local Bus = workspace:FindFirstChild(player.PlayerGui.TGUI.Value) if key == Enum.KeyCode.Q then -- LEFT SIGNAL Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false elseif key == Enum.KeyCode.E then -- RIGHT SIGNAL Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false elseif key == Enum.KeyCode.X then -- HAZARDS Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false elseif key == Enum.KeyCode.K then -- KNEEL Bus.Model.Bodykit.Blinkers.DriversCab.KneelEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.KneelEnable.Value elseif key == Enum.KeyCode.M then -- FRONT DOOR OPEN/CLOSE Bus.Model.Bodykit.Blinkers.DriversCab.FrontEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.FrontEnable.Value elseif key == Enum.KeyCode.N then -- BACK DOOR UNLOCK Bus.Model.Bodykit.Blinkers.DriversCab.RearEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.RearEnable.Value elseif key == Enum.KeyCode.L then -- HEADLIGHTS Bus.Model.Bodykit.Blinkers.DriversCab.HeadlightsEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HeadlightsEnable.Value elseif key == Enum.KeyCode.J then -- INTERIOR LIGHTS Bus.Model.Bodykit.Blinkers.DriversCab.InteriorEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.InteriorEnable.Value elseif key == Enum.KeyCode.R then -- REVERSE Bus.Model.Bodykit.Blinkers.DriversCab.ReverseEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.ReverseEnable.Value end end)
If anyone can please help me out that would be amazing!
The problem here is exactly what the error says: Bus
is nil
.
When the FindFirstChild
function cannot find the object it has been asked to look for, it returns nil
. Since you set Bus
to be a result of FindFirstChild
, there is a possibility of it being nil
.
Instead, you should use WaitForChild
, which will yield the script until the child exists, then the script will continue.
Hope this helps! :)
EDIT:
-- instead of this local Bus = game.Workspace:FindFirstChild(game.Players.LocalPlayer.PlayerGui.TGUI.Value) -- do this local player = game.Players.LocalPlayer local playerGui = player:WaitForChild("PlayerGui") local tGui = playerGui:WaitForChild("tGui") local Bus = workspace:WaitForChild(tGui.Value) -- or if your bus will always have the same name you can simply do local Bus = workspace:WaitForChild("Bus")