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
What is the issue?
The issue is that all players are stacking on each other when moved to the new position.
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)
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.
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?
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?
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!
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.