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)
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.
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)
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.
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)
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)
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?
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)