Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

For loop not recognizing Table Value?

Asked by 5 years ago
Edited 5 years ago

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)
0
See if you can do an else statement after all the elseif statements and print something. You should put it after the end on line 45 and properly indent to line it up with the elseif statements. If that prints then there must be something wrong with your if statements. zdragon100 0 — 5y
0
Added an else and print at the end. The print didn't print, it printed "Not Value1", and "Not Value2" 14x each. That's the amount of cars that the loop is sorting through for player1, and player2. Babyseal1015 56 — 5y
0
can you add the table insertion function you have? I have a few thoughts on how to make this more efficient, though to answer your problem, i'd need to see what the carHolder array is and how you're adding strings to the CarArray table SerpentineKing 3885 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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 models, 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)
0
Thank you so much!!! Babyseal1015 56 — 5y
Ad

Answer this question