I (not without the help of Assistant) have made a script which randomly picks a location out of 3 given ones. However it seems like the “random” choice isn’t at all random. Conversely, it is somehow based on distance, despite distance never being mentioned in the script as a whole.
Two of the teleport locations are located close to each other - when i use the teleport feature it alternates between these 2, ignoring the 3rd one, which is located really far away.
When i use the teleport feature near the third one, it teleports me to itself, but when i use it again it goes into an infinite loop of rerolling the same number (as it’s disallowed to output the same result twice in a row) until the script’s execution time runs out.
Any ideas?
local locations = {
}
for i, v in workspace.locations:GetDescendants() do
if v:IsA("Attachment") then
table.insert(locations, v.WorldCFrame.Position)
end
end
tool.Activated:Connect(function()
local function generateUniqueRandomNumber(min, max)
local newNumber
repeat
newNumber = locations[math.random(1, #locations)]
until newNumber ~= lastNumber
lastNumber = newNumber
return newNumber
end
local randomIndex = generateUniqueRandomNumber()
character.HumanoidRootPart.CFrame = CFrame.new(randomIndex)
end)
I have tried swapping simple things (such as teleporting you to the part as opposed to the attachment and vice versa) and adding some checks for whether the number is same or not. I also tried printing it which almost crashed my Studio instance.
All instances that are far away from the character are not available in the workspace, the client (local script) can’t access them. This is due to the Streaming Enabled setting or the level of graphics quality, as the client does not render instances that are far away for optimization purposes.
This might explain why the third location is never chosen, which cause it to alternates between the other two, as you have made it so that the same location cannot be selected two times in a row.
In this case, you need to manualy set the locations in the local script. You can also ask the server, but this include a time delay.
Well that explains it. What’s funny is that i actually considered the render distance to be an option.
Would you recommend using a bunch of coordinate strings instead?
If your game is small enough, you can consider disabling StreamingEnabled in the workspace. Otherwise, you could make a function that requests the server to do the random teleportation.
Or request the locations themselves from the server.