Math.random() works only one time

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to math.random() work more times after a use.
  2. What is the issue? Include screenshots / videos if possible!
    It works only one time after a use.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried clearing the values but that got even worse.

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local delay = false

local Randomchallenge = math.random(1, #ReplicatedStorage.RemoteEvents.Challenges:GetChildren())
local RandomObby = math.random(1, #ServerStorage.Obbies:GetChildren())

local CollectionService = game:GetService("CollectionService")
local KillParts = CollectionService:GetTagged("KillPart")
local TeleportLobbyParts = CollectionService:GetTagged("TeleportLobbyPart")

-- Portal touching

	-- Delay
	if delay == true then
		delay = false
		if hit.Parent:FindFirstChild("Humanoid") then

			local char = hit.Parent
			local plr = game.Players[char.Name]
			hit.Parent = char

			local Obby = plr:WaitForChild("stats").Obby
			--Randomizing and getting the results in values

			plr:WaitForChild("stats").Challenge.Value = ReplicatedStorage.RemoteEvents.Challenges:GetChildren()[Randomchallenge].Name
			plr:WaitForChild("stats").Obby.Value = ServerStorage.Obbies:GetChildren()[RandomObby].Name
			-- Teleporting to the random obbies

			ServerStorage.Obbies:FindFirstChild(Obby.Value).Parent = workspace.Obbies
			delay = true
			-- Cleaning Values
			Obby.Value = ""
			plr:WaitForChild("stats").Challenge.Value = ""

Note: This is not the full script.

More information: When player triggers portal script gets a random obby and teleports you to there, after the completion you get teleported back to lobby. When you want to do that again, everything breaks.

Thanks for helping if you do!

you have to put both of the Random variables inside the Touched Event.

1 Like