Script Issue, easy to fix

so i made a script that receives a remove event, this is the line:
SpawnCarEvent.OnServerEvent:Connect(function(player, carName)

CarName is associated to a car that you chose in an other script. is made a line that associates the cars names to image ids:
local CarImageIds = {

[6068169587] = "Beige Dune Buggy" ,
[109662003] = "Black Jeep" ,
[242143853] = "Black Sedan" ,
[8382532839] = "Blue SUV" ,
[126103845] = "Ford Shelby Truck" ,
[4072026855] = "Green camo Jeep" ,
[3142634] = "Lamborghini Aventador SVJ" ,
[7225067642] = "Pink Jeep" ,
[7141615506] = "Police Car" ,
[28261053] = "Red Maserati Car" ,
[6881212666] = "Red Sedan" ,
[7262556994] = "White Van" ,

}

how do i make the CarImageIds = the CarName
If you dont understand, i could explain, just tell me.
Heres the script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local DeleteCarEvent = ReplicatedStorage:WaitForChild("DeleteCar")
local CarSpawnNotificationEvent = ReplicatedStorage:WaitForChild("CarSpawnNotification")

local CarImageIds = {

	[6068169587] = "Beige Dune Buggy" ,
	[109662003] = "Black Jeep" ,
	[242143853] = "Black Sedan" ,
	[8382532839] = "Blue SUV" ,
	[126103845] = "Ford Shelby Truck" ,
	[4072026855] = "Green camo Jeep" ,
	[3142634] = "Lamborghini Aventador SVJ" ,
	[7225067642] = "Pink Jeep" ,
	[7141615506] = "Police Car" ,
	[28261053] = "Red Maserati Car" ,
	[6881212666] = "Red Sedan" ,
	[7262556994] = "White Van" ,

}







SpawnCarEvent.OnServerEvent:Connect(function(player, carName)
	local Car = ServerStorage:FindFirstChild("Cars"):FindFirstChild(carName)
	
	if Car then
		local clonedCar = Car:Clone()
		clonedCar.Name = player.Name .. 'sCar'
		clonedCar.Parent = game.Workspace
		clonedCar:MoveTo(player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector * 15)
		
	local CarImage = CarImageIds(carName)
		CarSpawnNotificationEvent:FireClient(carName)
	end
end)

DeleteCarEvent.OnServerEvent:Connect(function(player, Car)
	if Car then
		Car:Remove()
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar')
	if Car then
		Car:Remove()
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar')
		if Car then
			Car:Remove()
		end
	end)
end)
1 Like

Aren’t the car image ids already equal to the name of the car, or is there a change you want to do in the dictionary?

1 Like

wait a second i think i made a mistake

1 Like

im getting this error: ServerScriptService.CarSpawner:39: attempt to call a table value - Server - CarSpawner:39

on this line of code:
local CarImage = CarImageIds(carName)
CarSpawnNotificationEvent:FireClient(CarImage)

2 Likes

It would be;

local CarName = CarImageIds(carId)
2 Likes
local CarName = CarImageIds[carId]
2 Likes

Because the keys are the Ids, and the values are the names

local CarImageIds = {

	[6068169587] = "Beige Dune Buggy" , -- Identify with CarImageIds[6068169587]
	[109662003] = "Black Jeep" , -- Identify with CarImageIds[109662003]
	[242143853] = "Black Sedan" , -- etc,
	[8382532839] = "Blue SUV" ,
	[126103845] = "Ford Shelby Truck" ,
	[4072026855] = "Green camo Jeep" ,
	[3142634] = "Lamborghini Aventador SVJ" ,
	[7225067642] = "Pink Jeep" ,
	[7141615506] = "Police Car" ,
	[28261053] = "Red Maserati Car" ,
	[6881212666] = "Red Sedan" ,
	[7262556994] = "White Van" ,

}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local DeleteCarEvent = ReplicatedStorage:WaitForChild("DeleteCar")
local CarSpawnNotificationEvent = ReplicatedStorage:WaitForChild("CarSpawnNotification")

local CarImageIds = {

	[6068169587] = "Beige Dune Buggy" ,
	[109662003] = "Black Jeep" ,
	[242143853] = "Black Sedan" ,
	[8382532839] = "Blue SUV" ,
	[126103845] = "Ford Shelby Truck" ,
	[4072026855] = "Green camo Jeep" ,
	[3142634] = "Lamborghini Aventador SVJ" ,
	[7225067642] = "Pink Jeep" ,
	[7141615506] = "Police Car" ,
	[28261053] = "Red Maserati Car" ,
	[6881212666] = "Red Sedan" ,
	[7262556994] = "White Van" ,

}







SpawnCarEvent.OnServerEvent:Connect(function(player, carName)
	local Car = ServerStorage:FindFirstChild("Cars"):FindFirstChild(carName)
	
	if Car then
		local clonedCar = Car:Clone()
		clonedCar.Name = player.Name .. 'sCar'
		clonedCar.Parent = game.Workspace
		clonedCar:MoveTo(player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector * 15)

	local CarImage = CarImageIds(carName)
		CarSpawnNotificationEvent:FireClient(CarImage)
	end
end)

DeleteCarEvent.OnServerEvent:Connect(function(player, Car)
	if Car then
		Car:Remove()
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar')
	if Car then
		Car:Remove()
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar')
		if Car then
			Car:Remove()
		end
	end)
end)
1 Like

Alternatively, you can make a loop with pairs and the dictionary and when either the key or the value equal the desired result you return both values.

1 Like

Wait why’d you send the code again?

1 Like

because i made a mistake when i sent is first

this is the first one i sent

local CarImage = CarImageIds(carName)
CarSpawnNotificationEvent:FireClient(carName)

this is the second one i sent

local CarImage = CarImageIds(carName)
	CarSpawnNotificationEvent:FireClient(CarImage)

But that isn’t the issue, the issue is you are mixing the keys and values in the dictionary up. It’s a simple fix, either swap the values and keys, use a pairs loop to find a key from a value, or just using the image ids as the key, and the name as the value.

what would i do for the In Pairs?

This is how you would do it:

function getKeyFromValue(dictionary, value)

  for k, v in pairs(dictionary) do

    if v == value then

      return k
      break

    end

  end 

end

local CarId = getKeyFromValue(CarImageIds, CarName )
1 Like

im getting an error when i put your line

local CarName = CarImageIds[carId]
CarSpawnNotificationEvent:FireClient(carId)

What’s the error, and is carId defined?

they are red
Capture

1 Like

They are not defined, meaning you didn’t set a value for them, maybe you should do that.

what do i do, i don’t understand

1 Like

You need to declare and set a value for the variable carId

1 Like