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

[Player CFrame] Random Points Help?

Asked by 10 years ago

Teleporting to a random point on clicked!

points = game.Workspace.Points:GetChildren()
randPoint = math.random(1,#points)
player = script.Parent.Parent.Parent.Parent
script.Parent.MouseButton1Click:connect(function()
    player.Character.Torso.CFrame = player.Character.Torso.CFrame(randPoint.Position)
end)

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

Your problem is that you're just getting a random number between 1 and the number of children in 'Points'. You're not actually getting one of the parts. You have to index the 'points' table with the random number to get your part(:

Also, define the random point INSIDE the scope of the MouseButton1Click event, that way it will be random everytime you click.

Use a localscript, and use game.Players.LocalPlayer to get the player.

local points = workspace.Points:GetChildren()
local plr = game.Players.LocalPlayer --get player

script.Parent.MouseButton1Down:connect(function()
    local randPoint = points[math.random(1,#points)] --index with random number
    plr.Torso.CFrame = randPoint.CFrame * CFrame.new(0,3,0)
end)

Ad

Answer this question