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

Error: Attempt to Index Number with Number. What's the issue?

Asked by 2 years ago
Edited 2 years ago

So, I'm trying to do a script that involves not only races, but also another factor (Will of D), but that part is somewhat irrelevant. Each time I run the game, an error appears in Roblox Studio's output:

ServerScriptService.RaceScript:20: attempt to index number with number

Here's the script from line 15-33, and apparently the issue is in line 20. Anyone know the problem and solution to this?

17.local raceweight = 0

19.for racechancesdata in pairs(racechances) do

20.raceweight = raceweight + racechancesdata[2]

21.end

23.--Randomizing

24.function randomizerace()

25.local chance = math.random(1,raceweight)

26.local counter = 0

27.for racechancesdata in pairs(racechances) do

28.counter = counter + racechancesdata[2]

29.if chance <= counter then

30.return racechancesdata[1]

31.end

32.end

33.end

0
you expected racechancesdata to be a table on line 20 but racechancesdata is a number, did you possibly mean "for _, racechancesdata in pairs(racechances) do"? where _ is just name for a variable that's not used imKirda 4491 — 2y
0
when looping over table, pairs() gives you 2 variables, index and the value, index is position of value in the table, it's always a number imKirda 4491 — 2y
0
hey also 2022 you can remove pairs(), roblox made an alternative: "for _, racechancesdata in racechances do" imKirda 4491 — 2y
0
pairs() is cooler to read than "in" but I see you out here spitting facts greatneil80 2647 — 2y

Answer this question