Jump to content

[HELP]


Firespider

Recommended Posts

Hello! I have another problem I want to solve, if we press the arrow on the right, it goes to the other car, but for some reason it doesn't go to the fourth one.
 
 
 
bindKey("arrow_r", "Down", function()
destroyElement (Veh1)
if (car == 1) then
local Veh2 = createVehicle(411, 2154.45996, -1153.21362, 23.87550)
bindKey("arrow_r", "Down", function()
destroyElement (Veh2)
car = 1+1
end
)
if (car == 2) then
local Veh3 = createVehicle(478, 2154.45996, -1153.21362, 23.87550)
bindKey("arrow_r", "Down", function()
destroyElement (Veh3)
car = 1+2

end
)
if (car == 3) then
createVehicle(240, 2154.45996, -1153.21362, 23.87550)
end
end
end
end
)

 

Link to comment

You should carefully rethink your code design -- this may work for 1 or 2 car options, but as you add the 3rd, 4th, 5th etc, it gets really complicated unnecessarily. Instead, consider reusing existing code with iterations through a table and extracting parts of the code into their own functions.

Here's a much more flexible version of your code, which does not rely on adding numbers to variable names, writing a cascade of if blocks and bind handlers within bind handlers, etc.

local models = {
  411, 478, 240 -- add as many models here as you want, and the code will work without any other changes
}

local displayVehicle, index -- declaring variables as local ahead of time allows to get the speed benefits of `local` while having variables accessible thoughout the current script file (not accessible in other scripts of the same resource though)

local function updateDisplayVehicle()
  if (isElement(displayVehicle)) then -- to prevent generating errors when the vehicle doesn't exist
    destroyElement(displayVehicle) -- delete the existing vehicle if it exists
  end
  local vehModel = models[index] -- get the vehicle model corresponding to the current index
  displayVehicle = createVehicle(vehModel, 2154.45996, -1153.21362, 23.87550) -- spawns a vehicle with the correct vehicle model
end

local function tryChangeIndex(newIndex)
  if (models[newIndex]) then -- if the given index is valid (i.e. table `models` contains a value for that index)
    index = newIndex -- set current index to the given index
    updateDisplayVehicle() -- and update the currently spawned vehicle
  end
end

local function nextVehicle()
  tryChangeIndex(index + 1)
end
bindKey("arrow_r", "down", nextVehicle)

local function previousVehicle()
  tryChangeIndex(index - 1)
end
bindKey("arrow_l", "down", previousVehicle)

I've tried to provide comments that should help you understand what I'm doing in the code. I hope this helps.

Link to comment

@Addlibs

I have a problem, it's like a car shop and it's supposed to put the very first car down, but I don't know where the beginning of the whole thing starts, so to speak, because English is not my native language and I might have misread something. So, so to speak, which is the very first function that should be used in this situation. Thanks in advance for your answer!
 
 
 
Link to comment
local models = {
  411, 478, 240 -- add as many models here as you want, and the code will work without any other changes
}

local displayVehicle, index -- declaring variables as local ahead of time allows to get the speed benefits of `local` while having variables accessible thoughout the current script file (not accessible in other scripts of the same resource though)

local function updateDisplayVehicle()
  if (isElement(displayVehicle)) then -- to prevent generating errors when the vehicle doesn't exist
    destroyElement(displayVehicle) -- delete the existing vehicle if it exists
  end
  local vehModel = models[index] -- get the vehicle model corresponding to the current index
  displayVehicle = createVehicle(vehModel, 2154.45996, -1153.21362, 23.87550) -- spawns a vehicle with the correct vehicle model
end

updateDisplayVehicle()

local function tryChangeIndex(newIndex)
  if (models[newIndex]) then -- if the given index is valid (i.e. table `models` contains a value for that index)
    index = newIndex -- set current index to the given index
    updateDisplayVehicle() -- and update the currently spawned vehicle
  end
end

local function nextVehicle()
  tryChangeIndex(index + 1)
end
bindKey("arrow_r", "down", nextVehicle)

local function previousVehicle()
  tryChangeIndex(index - 1)
end
bindKey("arrow_l", "down", previousVehicle)

 

Link to comment
local models = {
  411, 478, 240 -- add as many models here as you want, and the code will work without any other changes
}

local displayVehicle, index -- declaring variables as local ahead of time allows to get the speed benefits of `local` while having variables accessible thoughout the current script file (not accessible in other scripts of the same resource though)

local function updateDisplayVehicle()
  if (isElement(displayVehicle)) then -- to prevent generating errors when the vehicle doesn't exist
    destroyElement(displayVehicle) -- delete the existing vehicle if it exists
  end
  local vehModel = models[index] -- get the vehicle model corresponding to the current index
  displayVehicle = createVehicle(vehModel, 2154.45996, -1153.21362, 23.87550) -- spawns a vehicle with the correct vehicle model
end

updateDisplayVehicle()

local function tryChangeIndex(newIndex)
  if not models[newIndex] then
	if newIndex < 1 then
		newIndex = #models
	else
		newIndex = 1
	end
  end
  
  index = newIndex
  
  updateDisplayVehicle()
end

local function nextVehicle()
  tryChangeIndex(index + 1)
end
bindKey("arrow_r", "down", nextVehicle)

local function previousVehicle()
  tryChangeIndex(index - 1)
end
bindKey("arrow_l", "down", previousVehicle)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...