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

How can I get my brick to use specified teleporting?

Asked by 9 years ago

How can I make it so when I click a brick it teleports be between random specified places instead of random teleporting such as below. I wanted it specified to specific places.

function onClicked(player) player.Character.Torso.CFrame = CFrame.new(math.random(40, 120), 6.1, math.random(100, 180)) end script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 answer

Log in to vote
0
Answered by 9 years ago

For this you can simply create a coordinate plane with the origin of it being where you clicked. I'm going to assume you want the random teleporting to only range between the X and Z axis (since it'd be on a 2D surface), so I'll give an example of that.

Getting the Vector3

First, we're going to want to get the Vector3 position of where the user clicks. For this, we can use the "p" property of CFrame, which is what Mouse.Hit returns.

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:wait()
local Mouse = Player:GetMouse()
local Torso = Char:WaitForChild('Torso')

local RangeOffsetXZ = 20 -- the random offset range in studs on the X and Z axis added to the origin position of the spawned click CFrame.

Mouse.Button1Down:connect(function()
    local MouseCF = Mouse.Hit -- CFrame of origin
    local Origin = MouseCF.p -- Vector3 of origin
    local NewPos = Origin + Vector3.new(math.random(-RangeOffsetXZ/2,RangeOffsetXZ/2),0,math.random(-RangeOffsetXZ/2,RangeOffsetXZ/2)) -- This will generate random X/Z coordinates between the origin, and the range.

    Torso.CFrame = CFrame.new(NewPos)
end)

Hope this helped, let me know if you have any questions.

Ad

Answer this question