Function not giving error but not working as expected

local Data = game:GetService("DataStoreService"):GetDataStore("carxda123  12a	a1a23123")

local PlatesData = game:GetService("DataStoreService"):GetDataStore("car1aa1112a3123")
		local letters = {"a", "b", "c","d", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
		local numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
		local usid = "GLOBAL";


_G["GamePlates"] = {}






function saveplates()
   if _G["GamePlates"][1] then
	print('yep yep potato chip')
	local save = _G["GamePlates"]
	PlatesData:SetAsync(usid, save)
	end
end

function GetPlates()
	local data = PlatesData:GetAsync(usid)
	if data then
_G["GamePlates"] = data
	else
		_G["GamePlates"] = {}
end
end

function SetPlateOnModel(PlayerName, InstanceVehicle)
	print("giving")
	local car
	if InstanceVehicle then
	car = InstanceVehicle
	else
	car = workspace:FindFirstChild(PlayerName.."'s Car")
	end
	local Plate = car.Model.Body:FindFirstChild("Plate")
	GetPlates()
	print('allgood...')
	if Plate then
		print('plate here...')
		local VehiclePlate = nil
		for i,v in ipairs(_G["GamePlates"]) do
			print(v[1]..v[2]..v[3])
			if v[2] == PlayerName and v[3] == InstanceVehicle.Name then
				VehiclePlate = v[1]
			end
		end
		
		print(VehiclePlate)
		if VehiclePlate ~= nil then
			print('found plate')
		Plate.Plate1.SurfaceGui.TextLabel.Text = VehiclePlate
		Plate.Plate1.SurfaceGui.TextLabel.Text = VehiclePlate
		elseif VehiclePlate == nil then
			print('nil')
			if InstanceVehicle then
			InstanceVehicle:Destroy()
			end
			game.ReplicatedStorage.EventsFolder.Notifications:FireClient(game.Players[PlayerName], "There was an error while spawning car.", 7)
		end
	end
end



		
		
function GivePlate(plr, VehicleModel)
			local plate
			local yepyep = false
			repeat
			plate = string.upper(letters[math.random(1, #letters)])..string.upper(letters[math.random(1, #letters)])..string.upper(letters[math.random(1, #letters)]).." "..numbers[math.random(1, #numbers)]..numbers[math.random(1, #numbers)]..numbers[math.random(1, #numbers)]
			yepyep = true
			for i,v in ipairs(_G["GamePlates"]) do
				if v.Plate == plate then
				yepyep = false
				end
				
			end	
			wait()
			until yepyep == true
			
			wait()
			if yepyep == true then
				GetPlates()
				
				table.insert(_G["GamePlates"],{Plate = plate ,Owner = plr.Name, Vehiclemodel = VehicleModel})
				saveplates()

			end
		end
	

	
	game:GetService("ReplicatedStorage").BuyCar.OnServerEvent:Connect(function(Player, Item)
		if Item then
		local ItemPrice = AllItems[Item]
		if not Data:GetAsync(Player.UserId.."-"..Item) then
			if Player['leaderstats']['Wallet'].Value >= ItemPrice then
				Data:SetAsync(Player.UserId.."-"..Item, {Color="Black"})
				GivePlate(Player, Item)
				end
				end
			end
		end)

		
			
		
		
		GetPlates()
		
		
			game:GetService("ReplicatedStorage").SpawnCar.OnServerInvoke = function(player,NameOfCar)
				
		if Data:GetAsync(player.UserId.."-"..NameOfCar) then
			print('okboomr')
		
		  local vehicleName = player.Name .. "'s Car"
  

SetPlateOnModel(player.Name, car)

			end
		end

Hello! So this is my code, basically, i removed some stuff to make it look more easy to understand, so, it should give the plates, but it looks like _G[“GamePlates”] It’s empty, I can’t find the error, it’s printing, but it’s not changing car plate, no errors, I don’t know why… help!! Looks like when it saves, if it was empty or when I call GetPlates() and try to print _G[“GamePlates”][1] it says nil… any idea? I’ve beentrying to fix this all day

OUTPUT:
https://gyazo.com/5e16fd6cd20cca3ca451772919f63f34


  1. Separating values in a print statement with a comma avoids the attempt to concatenate nil with nil error.
print(v[1], v[2], v[3])

--[[ or,
print(table.unpack(v))
]]

--[[ or,
table.foreach(v, print)
]]

  1. The way you save data in _G.GamePlates conflicts with how you use it. Your GivePlate function saves a table like this in _G.GamePlates:
--[[ from this line,

table.insert(_G["GamePlates"],{Plate = plate ,Owner = plr.Name, Vehiclemodel = VehicleModel})

]]

{
	Plate = plate,
	Owner = plr.Name, 
	Vehiclemodel = VehicleModel
}

You would have to compare v.Owner == PlayerName and v.Vehiclemodel == InstanceVehicle.Name, and set VehiclePlate to v.Plate.

Or, you can use an array, and save your table in _G.GamePlates like this:

{
	plate,
	plr.Name,
	VehicleModel
}

That would be compatible with your SetPlateOnModel function.

1 Like

You’re so right, how did I commited this basic error!!
also, do you consider this method good, or it’s kind of bad? if so then how would you do it
edit: I’m having a trouble here:

	local VehiclePlate = nil
		for i,v in ipairs(_G["GamePlates"]) do
			print(v[1]..v[2]..v[3])
			if v[2] == PlayerName and v[3] == InstanceVehicle.Name then
				VehiclePlate = v[1]
				print("Oki!")
			end
		end
		

it’s not printing oki or changing VehiclePLate variable :thinking: but it’s printing like plate owner and stuff, and the check should wokr

As a personal preference, I would use DataStore2 for saving your data:

Although this resource was created out of a need for data safety, it also caches data automatically and simplifies your code very quickly. Roblox’s DataStoreService also already does this for you.


You can use more verbose print statements, like instead of using print(v[1]..v[2]..v[3]), you could be printing how they compare to the values you want to compare against:

print(v[2], PlayerName, v[2] == PlayerName)
print(v[3], InstanceVehicle.Name, v[3] == InstanceVehicle.Name)

If the last value on both those prints are true, then the check should work.

Well, I think my checks should work, edit: o wait, it’s beause I’m changing name of cars, lol… thank you!!

1 Like