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

My problem is, this error keeps showing up: attempt to call local 'spawns' (a table value) ?

Asked by
Gololz 0
6 years ago

Okay, so, I am a beginner in coding on roblox and i am seriously stumped. I have specified a variable; local spawns = chosenmap:WaitForChild('Spawns'):GetChildren() then the first time it is used in the code; local randomspawn = spawns(allspawns) this error appears;
attempt to call local 'spawns' (a table value). I am very confused, I have tried removing pieces and it hasn't worked, I have even rewritten the whole thing and nothing has changed. I have been searching for help for a while now and I'm beginning to get annoyed. Please try your best to help,

Regards, Gololz

0
What do you expect spawns(allspawns) to do? Spawns is a table, and this piece of code makes no sense. Programical 653 — 6y

1 answer

Log in to vote
0
Answered by
movsb 242 Moderation Voter
6 years ago

You are getting this error because you are trying to call a table like how you would call a function; Your local variable 'spawns' is a table containing all of the children inside 'Spawns'; Lua thinks that you are trying to call spawns with the argument 'allspawns'; If you want to spawn a player at a random spawn inside of Spawns, than you should use math.random to get an index of each child inside the table; sounds confusing right? Well here how you can actually do this in your code:

math.randomseed(os.time()) --set the seed for the random number generator which helps to generate random numbers

--create a warm up function for math.random, so that you actually get a random value
local function WarmUp()
    math.random();
    math.random();
    math.random();
end

local spawns = chosenmap:WaitForChild("Spawns"):GetChildren();

WarmUp();

--generates a random number in between 1, and however many spawns you have inside of spawns
local random_spawn_index = math.random(1, #spawns);

local randomspawn  = spawns[random_spawn_index]; --now randomspawn is a random spawn inside of Spawns

Since you are a beginner in Lua I am not going to go into too much detail of what this is doing, but I will tell you that #spawns actually gets the length of the spawns table (i.e. how many elements/objects are inside of it), so if there are 10 spawns inside of Spawns, and we want to pick a random one, we just generate a random number between 1, and #spawns (in this case #spawns would be equal to 10)

I hope this helped you.

Ad

Answer this question