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

Error teleporting player to random brick?

Asked by
trecept 367 Moderation Voter
6 years ago
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?

0
is this a localscript or a serverscript? Hakurem 30 — 6y
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" Hakurem 30 — 6y

1 answer

Log in to vote
0
Answered by
Tomstah 401 Moderation Voter
6 years ago

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()))])
0
I think it worked, but I have a new error: bad argument #2 to 'random' (interval is empty) trecept 367 — 6y
0
Remove the first " 1, " and then only have the #(v:GetChildren())) Tomstah 401 — 6y
Ad

Answer this question