Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index field "?" (a number value)?

I made a script that chooses a random number between 1 and the amount of spawn points, but I can't get the player's Torso to move to the place I want. Any help?

Here's the script :

local button = script.Parent.spawnButton
local blackScreen = script.Parent.Parent.blackScreen
local spawned = game.Players.LocalPlayer.data.spawned
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local spawns = {}

for i, v in pairs(workspace.spawnPoints:GetChildren()) do
	table.insert(spawns, i)
	print("[SpawnCheck] Spawn " .. i .. " ready")
	wait()
end

button.MouseButton1Down:connect(function()
	for i = 1, 20 do
		blackScreen.BackgroundTransparency = blackScreen.BackgroundTransparency - .05
		wait(.025)
	end
	print("[SpawnCheck] Teleport started")
	local n = math.random(#spawns)
	humanoid.Parent.Torso.CFrame = CFrame.new(spawns[n].Position)
	print("[SpawnCheck] Teleport finished")
end)

The error message I get in the output is exactly this : 10:43:09.838 - Players.Player1.PlayerGui.main.rightSide.spawnHandler:20: attempt to index field '?' (a number value)

1 answer

✓ Accepted answer

Line 8:

table.insert(spawns, i)

That's incorrect. You're inserting the *index *into the table. You want to insert the value of that index, which is "v" in this case. Change that line to:

table.insert(spawns, v)

2 comments

0

Thank you. I'm pretty new to table functions

0

No problem! Just takes some practice.

This discussion is preserved as it was. New answers, comments, and votes are closed.