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

ServerScriptService.Scripts.Testing:5: '=' expected near 'in'?

Asked by 4 years ago
Edited 4 years ago

I have been trying to make a tower battles game for days now, but there is this little error that wont go away..

The error is the title.. Here is my script, read through it and tell me if something is wrong.

local enemyai = require(game.ServerScriptService.Modules.Classes.Enemy.EnemyAi)

local source = {}

for_.n in pairs(workspace.Course.Paths:GetChildren()) do source[n.Tag.Value] = n end

source["StartingPoint"]= workspace.Course.Other.StartPath

while wait() do spawn(function() enemyai.StartEnemy(source,"Regular") wait(3) end

0
Sorry the lines are a bit messed up, I copy and pasted it. xd xCroissxnt 0 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

In your script you did

for_.n in pairs(workspace.Course.Paths:GetChildren()) do 
    source[n.Tag.Value] = n 
end

You did "for _.n" instead of "for _, n"

Example :

for _, n in pairs(workspace.Course.Paths:GetChildren()) do 
    source[n.Tag.Value] = n 
end
0
Thank you for trying to help.. But it sadly doesn't work. xCroissxnt 0 — 4y
0
It still tells me the same thing xCroissxnt 0 — 4y
0
You also forgot a end) after calling spawn(function(). voidstaronyt 30 — 4y
0
I tried that, but now it says ServerScriptService.Scripts.Testing:15: 'end' expected (to close 'while' at line 11) near '<eof>' xCroissxnt 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

It's a syntax mistake. Putting a period instead of a comma tells Lua to check for an index in the given table, which requires an equal sign to be applied to a number before the index in the loop in a numeric for loop. For example, this is a generic for loop...

local numbers = {1, 2, 3}
for _, number in pairs(numbers) do
    print(number)
end

...but this is a numeric for loop:

local var = {number1 = 0, number2 = 0, number3 = 0}
for i = 1, var.number1 do
    print(i)
end

A comma is used in for loops to separate the two variables from each other. Using a period makes the compiler try to index the second variable.

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

As everyone else said:

It's a syntax mistake. Putting a period instead of a comma tells Lua to check for an index in the given table, which requires an equal sign to be applied to a number before the index in the loop in a numeric for loop. - BashCaster

And

You did "for _.n" instead of "for _, n" - voidstaronyt

But what your also forgetting is the "i" in "ipairs". The difference what it returns. "ipairs" returns index-value, while pairs returns key-value. You can read more about the difference between "ipairs" and "pairs" here which I found helpful in this explanation.

local enemyai = require(game.ServerScriptService.Modules.Classes.Enemy.EnemyAi)
local source = {}

for _, n in ipairs(workspace.Course.Paths:GetChildren()) do 
    source[n.Tag.Value] = n 
end

source["StartingPoint"] = workspace.Course.Other.StartPath

spawn(function() 
    while wait() do 
        enemyai.StartEnemy(source,"Regular") 
        wait(3) 
    end
end)

Also doing "while wait() do" before a spawn function is just bad practice (unless you have a really good reason for it) because its creating a new thread everytime it repeats its self, which eventually will cause performance issues.

0
Everything works except, ServerScriptService.Scripts.Testing:15: 'end' expected (to close 'while' at line 11) near '<eof>' xCroissxnt 0 — 4y
0
Yeah, I just edited it, I forgot a space between "for" and "_", if you try the code now it should work. SimplifiedCode 227 — 4y
0
Still a no. xd Still says ServerScriptService.Scripts.Testing:15:'end' expected (to close 'while at line 11) near '<eof>' xCroissxnt 0 — 4y
0
I never said it couldn't, but, that's MY answer and what I would do in this situation. SimplifiedCode 227 — 4y
0
Did it work? If it did, I'd appreciate if you marked my answer as "accepted". SimplifiedCode 227 — 4y

Answer this question