How to reroll RNG?

I’m trying to make a mopping system like in those cafe games. I have multiple models of spills for the player to clean up but sometimes it would waste an RNG because the spill already exists. How would I make it so the system rerolls to another spill if the one originally selected has already been spawned? I know it’s fixable with a table but I couldn’t find a post about it. (My brain has already rotted)

My code

local spillsFolder = script.Parent.Spills
local broom = game:GetService("ServerStorage"):WaitForChild("broom")
local event = game.ReplicatedStorage.RemoveInventory

local TweenService = game:GetService("TweenService")
local tInfo = TweenInfo.new(1)

local RNG = Random.new()

local function generateRNG()
	local choice = RNG:NextInteger(1, #spillsFolder:GetChildren())
	local spill = spillsFolder:GetChildren()[choice]
	return spill
end

local function acceptRNG(spillRNG)
	spillRNG:FindFirstChild("Main").Transparency = 0
	spillRNG:FindFirstChild("Part").Transparency = 0
	spillRNG:FindFirstChild("Glass").Transparency = 0.35
	spillRNG:FindFirstChild("Proximity").ProximityPrompt.Enabled = true
	spillRNG:FindFirstChild("Proximity").ProximityPrompt.Triggered:Connect(function(player)
		if player then
			local character = player.Character
			if character then
				local humanoid = character:FindFirstChild("Humanoid")
				if humanoid then
					if humanoid.Health > 0 then
						local broomClone = broom:Clone()
						broomClone.Parent = character
						game.Debris:AddItem(broomClone, 1)
						humanoid.WalkSpeed = 0
						event:FireClient(player)

						local fade_tween1 = TweenService:Create(
							spillRNG:FindFirstChild("Main"),
							tInfo,
							{Transparency = 1}
						)
						local fade_tween2 = TweenService:Create(
							spillRNG:FindFirstChild("Part"),
							tInfo,
							{Transparency = 1}
						)
						local fade_tween3 = TweenService:Create(
							spillRNG:FindFirstChild("Glass"),
							tInfo,
							{Transparency = 1}
						)
						fade_tween1:Play()
						fade_tween2:Play()
						fade_tween3:Play()
						spillRNG:FindFirstChild("Proximity").ProximityPrompt.Enabled = false

						wait(2.5)
						humanoid.WalkSpeed = 16
					end
				end
			end
		end
	end)
	spillRNG.Main.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid then
				if humanoid.Health > 0  and spillRNG.Proximity.CoolDown.Value == false and spillRNG.Main.Transparency == 0 then
					spillRNG.Proximity.CoolDown.Value = true
					humanoid.WalkSpeed = 0
					wait(1)
					humanoid.WalkSpeed = 16
					wait(1.5)
					spillRNG.Proximity.CoolDown.Value = false
				end
			end
		end
	end)
end

local function tradeRNG(spillRNG)
	
end

while wait(5) do
	local spillRNG = generateRNG()
	if spillRNG.Main.Transparency == 1 then
		acceptRNG(spillRNG)
	elseif spillRNG.Main.Transparency == 0 then
		tradeRNG(spillRNG)
	end
end
local spillRNG
while true do task.wait(5)
	repeat task.wait(0.1)
		spillRNG = generateRNG()
	until spillRNG.Main.Transparency == 1	
	acceptRNG(spillRNG)
end
1 Like
-- Repeat the rolling process until you find a spill that isn't
local function SpawnSpill()
  -- Make an empty variable for the sake of the loop
  spill = nil
  while not spill or spill.Transparency == 1 do
    spill = -- Call the roll function here and have it return the spill's part
  end
end

If you use that code, probably use another variable that counts how many rolls it has done and stops it if it doesn’t come to a conclusion, in the scenario that all the spills are visible.

For an alternate solution, you could use a table and just add/remove the spills to the table as they are spawned and cleaned up, respectively, and use table.find() to see if a potential spill is already visible.

I think the spill is transparent till he turns it to visible. So we are looking for the 1 not the 0.

1 Like

oh ya i didn’t notice lol thanks I’ll update the post

1 Like

Thanks, I didn’t know that was how the repeat and until worked.

1 Like

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