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

4 comments

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

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

0

hey also 2022 you can remove pairs(), roblox made an alternative: "for _, racechancesdata in racechances do"

0

pairs() is cooler to read than "in" but I see you out here spitting facts