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

Error teleporting player to random brick?

for i,v in pairs(game.Workspace:GetDescendants()) do
	if v.Name == "Spawnpoint" then
		local ChosenSpawn = (v[math.random(1, #v)])
		game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = ChosenSpawn.CFrame
	end
end

I'm trying to make a script so that it gets the descendants of the workspace to find all bricks named "Spawnpoint", and then making a random pick of one of the spawn bricks, and then teleporting the player to the brick.

Whenever I try to test the script in studio, I keep getting this error: attempt to get length of local 'v' (a userdata value) Any way I can fix this?

2 comments

0

is this a localscript or a serverscript?

0

May I ask how you make this into a format that is similar to what you get in studio on roblox. Btw 1st line i would do "for i,v in pairs(game.Workspace:GetChildren()) do"

1 answer

You're trying to use a length operator on an object.

local ChosenSpawn = (v[math.random(1, #v)])

I believe what you're wanting is:

local ChosenSpawn = (v[math.random(1, #(v:GetChildren()))])

2 comments

0

I think it worked, but I have a new error: bad argument #2 to 'random' (interval is empty)

0

Remove the first " 1, " and then only have the #(v:GetChildren()))

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