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

What does this index value mean? It keeps erroring every time I test.

Asked by 5 years ago
Edited 5 years ago

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!

1 answer

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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")
0
Should I use WaitForChild on the Local, Server, or both scripts on Bus? theawesomeSC4sim 0 — 5y
0
Both. You generally want to use it if its a necessary reference being made at the top of your script. chomboghai 2044 — 5y
0
So I've used it for both, then I got another error saying it's an infinite yield. What should I do> theawesomeSC4sim 0 — 5y
0
That's because your input to the WaitForChild command cannot be found. Most likely it's nil. You should make sure that game.Players.LocalPlayer.PlayerGui.TGUI.Value has a valid value, and don't index it using player.PlayerGui.TGUI. etc..., instead use more variables and WaitForChild's to make sure that each element exists. chomboghai 2044 — 5y
View all comments (2 more)
0
I edited the post to show you what I mean. chomboghai 2044 — 5y
0
I've tried both your methods, none of them seem to resolve the issue. I'm willing to show you parts of the vehicle so we can probably better identify the problem. My Discord is @mikenoza#5455 please reach me there. theawesomeSC4sim 0 — 5y
Ad

Answer this question