Repeat until and random bug

So basically i wanted it to load a random dream each time, and teleports player to a random spawn location in the map for EACH PLAYER

ive been seeing this bug alot and not sure but i think im always the only one which is the repeat loop leaving when the statement is false and also on the for loop for players it not generating a diffrent number for each player

i dont see anyone else going into the same problem as me in the repeat loop so idk whats going on its just (goes loop > does thing in the loop > no matter its true of false > leaves loop)

so far i tried the math.randomseed(tick()) and others still didnt work for the random
variables were checked but still couldnt figure out

ReplicatedStorage = game:GetService("ReplicatedStorage")
RunService = game:GetService("RunService")
EffectEvent = ReplicatedStorage.EffectEvent
IsRunning = true

wait(5)

while IsRunning == true do
	Players = game.Players:GetPlayers()
	Dreams = ReplicatedStorage.Dreams:GetChildren()
	PreviousDream = nil
	RandomDream = nil
	RandomSpawnLocation = nil
	NumberOfDreams = 0
	NumberOfSpawnLocations = 0
	for i,v in pairs(Dreams) do
		NumberOfDreams = NumberOfDreams + 1
	end
	repeat
		RandomDream = math.random(1,NumberOfDreams)
		print(RandomDream)
	until RandomDream ~= PreviousDream
	if RandomDream == PreviousDream then
		print("bug")
	end
	print("Finished Repeat Until", RandomDream)
	PreviousDream = RandomDream
	SelectedDream = Dreams[RandomDream]:Clone()
	SelectedDream.Parent = game.Workspace
	SpawnLocations = SelectedDream.SpawnLocations:GetChildren()
	for j,v in pairs(SpawnLocations) do
		NumberOfSpawnLocations = NumberOfSpawnLocations + 1
	end
	print(NumberOfSpawnLocations)
	for k,v in pairs(Players) do
		RandomSpawnLocation = math.random(1,NumberOfSpawnLocations)
		Player = Players[k]
		EffectEvent:FireClient(Player)
		Character = Player.Character or Player.CharacterAdded:Wait()
		HumanoidRootPart = Character.HumanoidRootPart
		HumanoidRootPart.Position = SpawnLocations[RandomSpawnLocation].Position
		print(RandomSpawnLocation)
	end
	wait(15)
	SelectedDream:Destroy()
end

Well, there is a lot wrong with this… Is this only supposed to happen when they join the game?

1 Like

Dont use math.random and math.randomseed!
I would recommand you using the Random datatype.

Example:

local Seed = 14234
local minrandom = 10
local maxrandom = 100
local Random = Random.new(seed)
Random:NextInteger(minrandom,maxrandom)

I went ahead and rewrote what you have a bit… study how its working so you get a better understanding of what is happening.

--If you don't add "local" infront of these variables, then they become globals... thats bad for memory usage.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerServ = game:GetService("Players")

local Dreams = ReplicatedStorage.Dreams:GetChildren() --Only needs done once.

local prevDreamIndex = {} --An array that will be keyed by player
local currentDream = {} --An array keyed by player


--Create a choose dream function to choose a new dream for the player
local function ChooseDream(player)

	--Choose any random dream to begin with
	local newDreamIndex = math.random(#Dreams)-- 1 to total number of dreams in the array
	
	--Check if the player even had a prev dream
	if(prevDreamIndex[player])then
		--Make sure to choose a new different dream
		repeat
			newDreamIndex = math.random(#Dreams)
			task.wait() --Just in case, to avoid timeouts
		until newDreamIndex ~= prevDreamIndex[player]
	end

	--Set the new prevDreamIndex to use
	prevDreamIndex[player] = newDreamIndex
	currentDream[player] = Dreams[newDreamIndex]:Clone()
	currentDream[player].Parent = game.Workspace --Not sure what exactly is happening with this. (Logically)
	
	EffectEvent:FireClient(player) --Fire off your event..
	
	--Get the players character
	local char = player.Character or player.CharacterAdded:wait()
	
	--Choose a random spawn location for the chosen dream
	local spawnLocations = currentDream[player].SpawnLocations:GetChildren()
	local chosenSpawn = spawnLocations[math.random(#spawnLocations)]
	
	--Move the character to the spawn location (ensure its above ground at chars hipheight)
	char:PivotTo(chosenSpawn:GetPivot()*CFrame.new(0, char.Humanoid.HipHeight, 0))
	
	task.wait(15)
	currentDream[player]:Destroy()
	currentDream[player] = nil --reset
end

--Starts when a new player is added to the game.
PlayerServ.PlayerAdded:Connect(ChooseDream)
1 Like

no, its on the server side and it does it for everyone to the same dream but diffrent spawn loacations, my problem was the maps were repeating sometimes and players were spawning in same loacations

So the scenario is… You port players from a lobby or something into a reserved server?
In the new server a random “Dream” is chosen. And each player that enters the server is sent to a different spawn location? Is this correct? I’m just trying to figure out the logical side of what you are trying to accomplish, so I can figure out the best way to help you. Maybe its all the same server and this a round based thing? The players are in the server already and you just need to spawn the map area and place them into it?

yes but i just found out the root problem was on the random thanks

Ok, glad you got it sorted out. :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.