Script returns results from table as "NIL"

Hey! So I’m making a car spawn GUI, and I’m using a module script to store car information in it. The script can be found below.

Error

What seems to be going wrong is that when I click a button from the GUI, it will give me an error like the one below:

Screen Shot 2022-04-07 at 1.53.21 PM


Scripts

  • Module Script (IN REPLICATED STORAGE)
local data = {}

local car_data_table = {
	["Police"] = {
		["Crown Victoria"] = {
			["Speed"] = "250 MPH",
			["Info"] = "Free car for all new police officers!"
		},
		["2019 Ford Explorer"] = {
			["Speed"] = "TBD",
			["Info"] = "TBD"
		},
		["2020 Ford Explorer"] = {
			["Speed"] = "TBD",
			["Info"] = "TBD"
		},
		["2016 Ford Interceptor"] = {
			["Speed"] = "TBD",
			["Info"] = "TBD"
		},
		["2019 Dodge Charger S1"] = {
			["Speed"] = "TBD",
			["Info"] = "TBD"
		},
		["2019 Dodge Charger S2"] = {
			["Speed"] = "TBD",
			["Info"] = "TBD"
		},
	},
}

local function getInfo(info)
	if info == "Victoria" then
		local speed = car_data_table.Police["Crown Victoria"].Speed
		local Info = car_data_table.Police["Crown Victoria"].Info
		return tostring(speed),tostring(Info)
	elseif info == "Intercept" then
		local speed = car_data_table.Police["2016 Ford Interceptor"].Speed
		local Info = car_data_table.Police["2016 Ford Interceptor"].Info
		return tostring(speed),tostring(Info)
	elseif info == "EXP1" then
		local speed = car_data_table.Police["2019 Ford Explorer"].Speed
		local Info = car_data_table.Police["2019 Ford Explorer"].Info
		return tostring(speed),tostring(Info)
	elseif info == "EXP2" then
		local speed = car_data_table.Police["2020 Ford Explorer"].Speed
		local Info = car_data_table.Police["2020 Ford Explorer"].Info
		return tostring(speed),tostring(Info)
	elseif info == "DC1" then
		local speed = car_data_table.Police["2019 Dodge Charger S1"].Speed
		local Info = car_data_table.Police["2019 Dodge Charger S1"].Info
		return tostring(speed),tostring(Info)
	elseif info == "DC2" then
		local speed = car_data_table.Police["2019 Dodge Charger S2"].Speed
		local Info = car_data_table.Police["2019 Dodge Charger S2"].Info
		return tostring(speed),tostring(Info)
	end
end

function data.GetCarInfo(info)
	getInfo(info)
end

return data
  • Local Script (IN GUI)
local player = game.Players.LocalPlayer
local spawner = player.PlayerGui:WaitForChild("PlayerUI")["Car Spawner"]
local carList = spawner.Cars
local carInfo = spawner["Car Info"]
local CarData = require(game:GetService("ReplicatedStorage").Car_Data)

-- Check if any cars from the list are pressed --

for _, item in pairs(carList:GetChildren()) do
	if item:IsA("TextButton") then
		item.MouseButton1Click:Connect(function()
			print("Pressed")
			if carInfo.Visible ~= true then
				carInfo.Visible = true
			end
			
			if item.Name == "Victoria" then
				carInfo.Car_Image.Image = "rbxassetid://9303376115"
				carInfo.Title.Text = "Crown Victoria"
				carInfo.Speed.Text = CarData.GetCarInfo("Victoria")
			elseif item.Name == "Interceptor" then
				carInfo.Car_Image.Image = "rbxassetid://9304101767"
				carInfo.Title.Text = "2016 Ford Interceptor"
				carInfo.Speed.Text = CarData.GetCarInfo("Intercept")
			elseif item.Name == "Explorer1" then
				carInfo.Car_Image.Image = "rbxassetid://9304121900"
				carInfo.Title.Text = "2019 Ford Explorer"
				carInfo.Speed.Text = CarData.GetCarInfo("EXP1")
			elseif item.Name == "Explorer2" then
				carInfo.Car_Image.Image = "rbxassetid://9304126405"
				carInfo.Title.Text = "2016 Ford Explorer"
				carInfo.Speed.Text = CarData.GetCarInfo("EXP2")
			elseif item.Name == "DC1" then
				carInfo.Car_Image.Image = "rbxassetid://9304130205"
				carInfo.Title.Text = "2019 Dodge Charger S1"
				carInfo.Speed.Text = CarData.GetCarInfo("DC1")
			elseif item.Name == "DC2" then
				carInfo.Car_Image.Image = "rbxassetid://9304133024"
				carInfo.Title.Text = "2019 Dodge Charger S2"
				carInfo.Speed.Text = CarData.GetCarInfo("DC2")
			end
		end)
	end
end

What seems to be going on?

3 Likes

in the module its never returned what the getInfo(info) is so the script isnt grabbing anything

function data.GetCarInfo(info)
    return getInfo(info)
end

i think this might fix it

2 Likes

This does fix it, but only the speed of the car. The info is still NIL

function data.GetCarInfo(info)
local speed,info = getInfo(info)
return speed,info
end
1 Like

It’s still only the speed, not the info.

The function returns two values; the script will assign the first one because TextLabel.Text only takes one value

local _, info = CarData.GetCarInfo(car)
-- 'info' is the information you want

How would I get the 2 separate values using this method? I have both speed and info returning from the function.

You would have to define the return value with two variables:

local speed, info = CarData.GetCarInfo(car)
-- speed is the first returned value
-- info is the second returned value
1 Like

:ok_hand: Perfect! Thank you @bigploom and @HugeCoolboy2007 for helping!