Script doesn't seem to be able to find a value

as the title suggests, the script seems unable to find the value and also seems to ignore my if statements.

The value is not nil and should be the same in the script.

task.wait(.1)

local IGNITION_KEYBOARD = Enum.KeyCode.F
local IGNITION_CONTROLLER = Enum.KeyCode.ButtonB

local UserInputService = game:GetService("UserInputService")
local gamepadPresent = UserInputService.GamepadEnabled

local veh = game.Workspace:FindFirstChild(script.Parent.Parent.Car.Value, true)

repeat task.wait() until script.Parent.Parent.Car.Value ~= nil

local function onInput(input:InputObject, gameProcessedEvent:boolean)
	if gameProcessedEvent then return end
	if not (input.KeyCode == IGNITION_KEYBOARD or input.KeyCode == IGNITION_CONTROLLER) then return end
	if script.Parent.Parent.IsOn.Value and script.Parent.Parent.Car.Value ~= nil then
		if input.UserInputState == Enum.UserInputState.End then return end
		script.Parent.Parent.IsOn.Value = false
		veh.DriveSeat.Ignition.IgnitionChange:FireServer("Off")
	else
		script.Parent.Parent.Starting.Value = input.UserInputState == Enum.UserInputState.Begin
	end
end

local function update()
	gamepadPresent = UserInputService.GamepadEnabled
	script.Parent.Text = "Hold <i><stroke color='#000000' thickness='2'><font weight='900'>"..(gamepadPresent and IGNITION_CONTROLLER.Name or IGNITION_KEYBOARD.Name).."</font></stroke></i> to turn on."
	script.Parent.Visible = not script.Parent.Parent.IsOn.Value
	if script.Parent.Parent.IsOn.Value and script.Parent.Parent.Car.Value ~= nil then
		print(veh)
		veh.DriveSeat.Ignition.IgnitionChange:FireServer("On")
	elseif script.Parent.Parent.Car.Value ~= nil then
		print(veh)
		veh.DriveSeat.Ignition.IgnitionChange:FireServer("Off")
	end
end

UserInputService.InputBegan:Connect(onInput)
UserInputService.InputChanged:Connect(onInput)
UserInputService.InputEnded:Connect(onInput)

UserInputService.GamepadConnected:Connect(update)
UserInputService.GamepadDisconnected:Connect(update)
script.Parent.Parent.IsOn.Changed:Connect(update)
update()

Output:

  22:25:24.236  Players.lenovo17101.PlayerGui.A-Chassis Interface.Ignition.Ignition:31: attempt to index nil with 'DriveSeat'  -  Client - Ignition:31
  22:25:24.236  Stack Begin  -  Studio
  22:25:24.236  Script 'Players.lenovo17101.PlayerGui.A-Chassis Interface.Ignition.Ignition', Line 31 - function onInput  -  Studio - Ignition:31
  22:25:24.236  Stack End  -  Studio
  22:25:24.236  nil  -  Client - Ignition:45
  22:25:24.237  Players.lenovo17101.PlayerGui.A-Chassis Interface.Ignition.Ignition:46: attempt to index nil with 'DriveSeat'  -  Client - Ignition:46
  22:25:24.237  Stack Begin  -  Studio
  22:25:24.237  Script 'Players.lenovo17101.PlayerGui.A-Chassis Interface.Ignition.Ignition', Line 46 - function update  -  Studio - Ignition:46
  22:25:24.237  Stack End  -  Studio
  22:25:25.471  nil  -  Client - Ignition:42
  22:25:25.471  Players.lenovo17101.PlayerGui.A-Chassis Interface.Ignition.Ignition:43: attempt to index nil with 'DriveSeat'  -  Client - Ignition:43
  22:25:25.472  Stack Begin  -  Studio
  22:25:25.472  Script 'Players.lenovo17101.PlayerGui.A-Chassis Interface.Ignition.Ignition', Line 43 - function update  -  Studio - Ignition:43
  22:25:25.472  Stack End  -  Studio

The script that changes the value on the server side:

script.IgnitionChange.OnServerEvent:Connect(function(text)
	print("e")
	if text == "Off" then
		script.IgnitionValue.Value = false
	elseif text == "On" then
		script.IgnitionValue.Value = true
	end
	
end)

If anything else is needed please ask!

1 Like

You’re misinterpreting the error.

The error is saying that veh is nil, hence

local veh = game.Workspace:FindFirstChild(script.Parent.Parent.Car.Value, true)
is returning nil.

I believe that you should be doing this:

local veh = script.Parent.Parent.Car.Value

instead, as it is an object value

2 Likes

yeah that did it. Although now I have an issue with the other script that is changing it on the server side.

script.IgnitionChange.OnServerEvent:Connect(function(text)
	print("e")
	if text == "Off" then
		script.IgnitionValue.Value = true
	elseif text == "On" then
		script.IgnitionValue.Value = false
	end
	
end)

image
It’s being triggered by the event, just not changing the value.

Should I use true/false instead?

1 Like

Try printing out text rather than “e” to see why the if statement isn’t working.

(You can also just change it to true and false)

1 Like

Actually I just noticed an issue in your code:

script.IgnitionChange.OnServerEvent:Connect(function(text)

should be

script.IgnitionChange.OnServerEvent:Connect(function(player, text)

as the first parameter is always the player who called the event

1 Like

All of these combined made it work!

Thank you!

1 Like

No problem :slight_smile:

(Also, you should mark my original reply as the solution, as it was the solution to the problem you had in the original post, rather than my most recent reply, which was a secondary problem)

3 Likes

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