So I'm trying to write a function that spawns cars for people. It's set up like a flood escape elevator. People can enter and equip a car and when the elevator moves down, it spawns everyones cars. I have a table that when people equip a car it puts the cars name as a string value into the table. The table is called CarArray. I used a couple of prints. The strings show up in the table. But the cars still wont spawn. No errors are printed and every print shows except anything after:
if value == CarArray[1] then
Any mistakes that you can see? Formatting or syntax or anything wrong?
local function SpawnCar() --Spawns Car local carHolder_forLoop = carHolder:GetChildren() local carStep = 0 local spawnPose = St1.Value + Vector3.new(0,5,0) repeat carStep = carStep + 1 for i = 1, #carHolder_forLoop do local value = carHolder_forLoop[i] if carStep == 1 then if value == CarArray[1] then print("CarArray[1]") local Clone = value:Clone() Clone.Parent = game.Workspace Clone:MoveTo(St1.Value + Vector3.new(0,5,0)) else print("Not Value1") end elseif carStep == 2 then if value == CarArray[2] then local Clone = value:Clone() Clone.Parent = game.Workspace Clone:MoveTo(St2.Value + Vector3.new(0,5,0)) else print("Not Value2") end elseif carStep == 3 then if value == CarArray[3] then local Clone = value:Clone() Clone.Parent = game.Workspace Clone:MoveTo(St3.Value + Vector3.new(0,5,0)) else print("Not Value3") end elseif carStep == 4 then if value == CarArray[4] then local Clone = value:Clone() Clone.Parent = game.Workspace Clone:MoveTo(St4.Value + Vector3.new(0,5,0)) else print("Not Value4") end end end until carStep == #CarArray or carStep == 4 end
carHolder is a folder in replicatedStorage that holds all of the car models. there is a local script in Starter gui that lets you equip a car and when you do it fires a remote event called "Equip". It sends the string value of the car [Its name is the string], and it says what what player equiped it so it know what spot to put it in CarArray, since the elevator only holds 4 player at a time but only needs 2 to start the race timer. Also if the player does not equip a car it selects a random car that they own so that what ranCar is.
CarArray Adding function:
equip.OnServerEvent:Connect(function(player, ranCar, plr_Name) --Deals with Car Equipping if PlayerArray[1] == plr_Name then CarArray[1] = ranCar elseif PlayerArray[2] == plr_Name then CarArray[2] = ranCar elseif PlayerArray[3] == plr_Name then CarArray[3] = ranCar elseif PlayerArray[4] == plr_Name then CarArray[4] = ranCar end end)
Improvements
Use in pairs
to run through tables with the index and value defined rather than separating the definition of the index and value
Issues
the carHolder's children are model
s, but you are attempting to compare them to a string
, which will ALWAYS result in a false
bool. instead, compare the string to the model's Name
and it should work as expected
Revised Server Script #1
local function SpawnCar() local carStep = 0 local offset = Vector3.new(0, 5, 0) local spawnPose = St1.Value + offset repeat carStep = carStep + 1 for i , model in pairs(carHolder:GetChildren()) do if model.Name == CarArray[carStep] then print("CarArray["..carStep.."]") local Clone = model:Clone() Clone.Parent = workspace if carStep == 1 then Clone:MoveTo(St1.Value + offset) elseif carStep == 2 then Clone:MoveTo(St2.Value + offset) elseif carStep == 3 then Clone:MoveTo(St3.Value + offset) elseif carStep == 4 then Clone:MoveTo(St4.Value + offset) end else print("Not Value"..carStep) end end until carStep == #CarArray or carStep == 4 end
I'm not sure if the equip event's PlayerArray contains the player instances or simply the player's name. If its the player's name then the below is alright
*Revised Server Script #2A"
equip.OnServerEvent:Connect(function(player, ranCar, plr_Name) for index, plr in pairs(PlayerArray) do if plr == plr_Name then CarArray[index] = ranCar end end end)
if the PlayerArray's vaues are the player instances, then instead use
*Revised Server Script #2B"
equip.OnServerEvent:Connect(function(player, ranCar, plr_Name) for index, plr in pairs(PlayerArray) do if plr.Name == plr_Name then CarArray[index] = ranCar end end end)