Teleport Randomly with the Random Function

Hello it is me InquistorWasBanned again. For some reason the Random function is not working for me. I am trying to make an object that teleports a player on touch with a click detector randomly around the map to be fair. But it does not work. Why?

print("Hello world!")
local box = script.Parent
local player = game.Players.LocalPlayer
local Random1 = math.random(239.88, -223.79)
local Random2 = math.random(-10260.82, -10501.66)

local function onClick(player)
	player.Character:MoveTo(Vector3.new(Random1, 237.039, Random2))
end
box.ClickDetector.MouseClick:Connect(onClick)

1 Like

Swap;

	player.Character:MoveTo(Vector3.new(Random1, 237.039, Random2))

for;

	player.Character.HumanoidRootPart.CFrame = CFrame.new(Random1, 237.039, Random2)
3 Likes

You don’t use MoveTo() to teleport the player. This is used primarily to make the player walk to a certain location. The reason why it didn’t work is because, well, it’s the player, and how the player moves is already determined by another script, and using MoveTo while that script is on will lead to issues. Instead, you modify the CFrame of the HumanoidRootPart to teleport the player.

Something like what yLocal suggested should work for what you are trying to do, although you can modify the CFrame a bit further to make the teleportation less weird.

1 Like

Thanks So Much!! I will try that.

1 Like

Its because the “randomness” will be the same every time. You need to recalculate the random position every time you click the click detector, instead of just calculating it once.

local box = script.Parent
local player = game.Players.LocalPlayer

local function onClick(player)
       local Random1 = math.random(239.88, -223.79)
       local Random2 = math.random(-10260.82, -10501.66)

	player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(Random1, 237.039, Random2)
end
box.ClickDetector.MouseClick:Connect(onClick)

1 Like

True too. But every time wouldn’t it change for different players.

1 Like

Well every player would have a different random position to go do. But what if the player were to hit the click detector twice?

1 Like

Hm that can not happen once they teleport unless they click two times really quickly which would be really hard. I can make a new block if I need touse it later perhaps.

2 Likes

Still does not work for some reason.

print("Hello world!")
local box = script.Parent
local player = game.Players.LocalPlayer
local Random1 = math.random(239.88, -223.79)
local Random2 = math.random(-10260.82, -10501.66)

local function onClick(player)
	player.Character.HumanoidRootPart.CFrame = CFrame.new(Random1, 237.039, Random2)
end
box.ClickDetector.MouseClick:Connect(onClick)
2 Likes

Is it outputing an error message?

You do need to check if the player’s character is loaded or when there is a HumanoidRootPart to manipulate.

1 Like

No there are no erros for some reason. I can send you a screenshot.

1 Like

![image|690x388]

1 Like

You are still generating only one random position.
Generate new random numbers each click.

print("Hello world!")
local box = script.Parent
local player = game.Players.LocalPlayer
-- removed

local function onClick(player)
	-- moved here
	local Random1 = math.random(239.88, -223.79)
	local Random2 = math.random(-10260.82, -10501.66)
	player.Character.HumanoidRootPart.CFrame = CFrame.new(Random1, 237.039, Random2)
end
box.ClickDetector.MouseClick:Connect(onClick)
1 Like

Okay Thanks let me try that right now.

1 Like

It still does not work for some reason.

1 Like

That’s strange. I tried out the script, put it in a Part with a ClickDetector in it, clicked it with a character and got one of the Randoms to tell me its arguments are backwards.

Yet you say the script does nothing at all! You should be getting errors!

Are you sure you’re clicking the right part?
Is that Hello World at the top printing at all?

1 Like

Let me check right now. Will be back.

1 Like


I see yep the random function is wrong! But why?

1 Like

Yes, swap the two numbers in the random, so that the first number is lower than the second number.

	-- from
	local Random1 = math.random(239.88, -223.79)
	local Random2 = math.random(-10260.82, -10501.66)
	-- to
	local Random1 = math.random(-223.79, 239.88)
	local Random2 = math.random(-10501.66, -10260.82)
2 Likes

Okay Thanks so Much! let me try it!

1 Like