Math.random not working with MoveTo and Vector3.new

  1. What do you want to achieve?
    I’m trying to move/teleport all players to a part when that part is touched by one player

  2. What is the issue?
    The issue is that all players are stacking on each other when moved to the new position.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes,
    I have tried multiple ways found on youtube using remote events and MoveTo however they are outdated. The only one that seems to work is listed below in the code block and the image attached.
    I have tried to create an offset using vector3 but it never checks the vector3 argument it just moves all characters to the position of the part.
    So it seems the issue is I can’t get math.random to work and if it does each player will need their own random number

local debounce = false
local Newposition = workspace.Scripts.Cutscenes.CourtroomCutscene
local xOffset = math.random (-10,10)
local zOffset = math.random (-10,10)

	script.Parent.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChild("Humanoid") then
				if debounce == false then
					debounce = true
			for i, players in pairs(game.Players:GetPlayers()) do
				players.Character:MoveTo(Newposition.Position + Vector3.new(xOffset,0,zOffset))
				print(Newposition.Position)
				wait(1)
					end
				end
			end
		end)
1 Like

Notice that you used math.rad() instead of math.random()

Yes sorry, It was originally math.random but I’m trying everything I can think of to get it to work

Could you please show what Newposition is ?

It’s because you generate a random number only once, at the beggining of your script.
When you’re referreing to zOffset/yOffset you don’t perform a math.random call, you get the previously generated value.

1 Like


It is just a part

Thanks, I will need to look up what you mean by calling math.random each time however upon multiple testing the math.random part doesn’t even seem to be working at all.
Each test I end up in the same position as shown in the output of the screenshot.
I know MoveTo can read position values but is there something wrong with the plus Vector3 request?

What he meant is -

If you put the math.random inside the event, that’d make sure, that you’ll get a random number each time the event is fired.

When the math.random is outside of the event, it won’t do that, but only choose a random number, but won’t change it.

No, what you did with MoveTo() seems legit and working fine

Right ok, so math.random will only ever change its values if fired from a remote event?

At the moment I’m not using a remote event and just having the players move from one position to another specified in the script.

No, no need to use remote events. Even if you have a Touched event, or a loop, make sure the math.random is inside of it.

Move these 2 lines to inside of the .Touched event

2 Likes

Thank you! that fixed it and now the players spread out after being moved.

Was it cause I declared them as a local variable or cause I did not make them global? or can math.random just not be used outside of an event for different outcomes?

It was because you put them outside of the event, which resulted in :

The random number would be chosen only once.

Because you put it inside the touched event, it’ll now choose a number each time a player touched it

ok but the debounce should only let one player touch it once thus not letting it trigger again till debounce is false again, anyway it’s working now thanks for your help! :heart:

1 Like

No problems :slight_smile:

You can always play with stuff

The debounce doesn’t change anything.
Look:

local randomNumber = math.random(1, 100)

for _ = 1, 10 do
  print(randomNumber)
end
for _ = 1, 10 do
  local randomNumber = math.random(1, 100)
  print(randomNumber)
end
local randFunc = math.random

for _ = 1, 10 do
  print(randFunc(1, 100))
end

Run these scripts separately and you’ll see the difference. In the first case you generate a random number once and you don’t overwrite/change it anywhere.
In the second case you generate a new number every interation of the loop so it’s different each time.
In the third case you assign math.random function to a variable and then you use this variable to get a new number each iteration.
The numbers will also be different.