Deleted random value gets still picked

Hello, sorry if I have chosen the wrong title but I don’t really know what to put there.

So what I try to do:
I try to copy a teleporter from Workspace and move it 20 studs (and that works). The teleporter teleports to a random minigame. The random minigames are in a folder in ReplicatedStorage called “games” In that folder are StringValue’s with the name of the minigames. In that StrinValue there is an NumberValue with the id of the minigame to teleport to. (there are currently 3 minigames)

If I run the script → it will clone the teleporter → move it 20 studs → gives the teleporter the game name and de game id to teleport. After that it will delete that game that has been chose so it can’ t be picked twice.

The problem:
If I run the script, sometimes it works perfect, but sometimes not. then it will pick a game, and copy it, and delete the game (StringValue). but for the next portal copy, it choses the exact same game (even if its deleted). and ofcourse, it gives then an error that it can’t see the game id because the game isn’t in the folder anymore

The question:
Is there a problem with the writing of the script or with how i deal with it?

The script:

local teleporter = game.Workspace.Teleporter

local games = game.ReplicatedStorage.games:GetChildren()

local distance = script.distance.Value
local counter = 1


wait(5)


while counter <= 3 do
	distance = distance + 20

	local clone = teleporter:Clone()
	clone.Parent = game.Workspace
	
	local randomgame = games[math.random(1, #games)]
	
	local gamename = randomgame.Name
	local gameID = randomgame.GameID.Value
	
	clone.Teleporter.BillboardGui.TextLabel.Text = gamename
	clone.Teleporter.gameID.Value = gameID
	
	randomgame:Destroy()
	
	clone.CFrame -= Vector3.new(distance)
	clone.Teleporter.CFrame -= Vector3.new(distance)

	counter = counter + 1
end

teleporter:Destroy()

Hopefully you understand what I try to say, and if you don’t, ask for more information and I will try to give you the information you need! :slight_smile:

If you get to this point, thanks for reading all of it and hopefully helping me,
Regards,
debestevanal

This line loads a reference to all the games (children of ReplicatedStorage.games) into the games variable. Destroying the game inside the while loop won’t change the list of games stored in that variable. Since there is a stored reference to each game, it maybe isn’t completely destroying a game when you ask it to (or at least there is still a ref to the unparented game in memory).

You’ll need to recheck the children of the games folder each time you destroy a game. Or, update the games variable once something is deleted (by explicitly removing the deleted game from this list or by calling get children again).

Anyway, that looks to be the main problem. Essentially, try refreshing the games held in the games variable at the time you need to pick one rather than having the list of games stored in a static script variable.

1 Like

Yes that was the problem!

Thank you for helping me :smile:

1 Like