Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Random seems to not be 'random?'

Greetings, I'm making a Region3-based sound system which checks if you're in an area, and maybe changes the skybox, or plays music. However, when I do play the music, it's from a table of music. My 'problem,' if you will, is that it only appears to play one sound and that only. It may just be my luck, but maybe it's something more. Feedback on anything greatly appreciated!

--LocalScript in StarterGui
local skyboxes = game.Lighting:WaitForChild("Skyboxes")
local skybox = game.Lighting:WaitForChild("Skybox")
local RunService = game:GetService("RunService")

local def_sbs = skyboxes:WaitForChild("Default")
local snow_sbs = skyboxes:WaitForChild("Snow")


local terrain = workspace:WaitForChild("Terrain")
local skc = terrain:WaitForChild("SkyBoxChangers")

--//Snowy Sky\\--
local snow_part1 = skc:WaitForChild("SnowPart1")
local snow_part2 = skc:WaitForChild("SnowPart2")
local snow_region = Region3.new(snow_part1.Position, snow_part2.Position)

--// Food Joint \\--
local canScan = true
local fastfood_part = game.Workspace.World:WaitForChild("Food Joint")
fastfood_part = fastfood_part:WaitForChild("Barrier")
local fast_food_region = Region3.new(fastfood_part.Position - (fastfood_part.Size/2), fastfood_part.Position + (fastfood_part.Size/2))

function playTune()
	if canScan then
		canScan = false
		local music = {}
		for i, v in pairs(script:GetChildren()) do
			if v:IsA("Sound") then
				table.insert(music, v)
			end
		end
		local playing
		for i, v in pairs(music) do
			if v.Playing then
				playing = true
				break
			end
		end
		if playing then
			return false
		else
			local be = music[math.random(#music)]
			return be.Name
		end
	end
end

RunService.RenderStepped:Connect(function()
	local char = game.Players.LocalPlayer.Character
	local found
	local region_present
	--Snow Region checking
	local snowr3_parts = workspace:FindPartsInRegion3WithWhiteList(snow_region, char:GetDescendants())
	for i, v in pairs(snowr3_parts) do
		if v:FindFirstAncestor(char.Name) then
			found = true
			region_present = "Snow"
			break
		end
	end
	--Food Joint Region checking
	local ffr3_parts = workspace:FindPartsInRegion3WithWhiteList(fast_food_region, char:GetDescendants())
	for i, v in pairs(ffr3_parts) do
		if v:FindFirstAncestor(char.Name) then
			found = true
			region_present = "Fast Food"
			break
		end
	end

	--[[ Checking ]]--
	
	if found and region_present then
		if region_present == "Snow" then
			skybox.SkyboxBk = snow_sbs.SkyboxBk
			skybox.SkyboxDn = snow_sbs.SkyboxDn
			skybox.SkyboxFt = snow_sbs.SkyboxFt
			skybox.SkyboxLf = snow_sbs.SkyboxLf
			skybox.SkyboxRt = snow_sbs.SkyboxRt
			skybox.SkyboxUp = snow_sbs.SkyboxUp
		end
		if region_present == "Fast Food" then
			char.Humanoid:UnequipTools()
			for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
				v.Parent = game.Players.LocalPlayer.Backpack2
			end
			local playing = playTune()
			if playing then
				script[playing]:Play()
				for i = 1, 10 do
					script[playing].Volume = script[playing].Volume + 0.1
					wait()
				end
				script[playing].Volume = 1
				
			end
			
		end
	elseif not found then
		skybox.SkyboxBk = def_sbs.SkyboxBk
		skybox.SkyboxDn = def_sbs.SkyboxDn
		skybox.SkyboxFt = def_sbs.SkyboxFt
		skybox.SkyboxLf = def_sbs.SkyboxLf
		skybox.SkyboxRt = def_sbs.SkyboxRt
		skybox.SkyboxUp = def_sbs.SkyboxUp
		for i, v in pairs(game.Players.LocalPlayer.Backpack2:GetChildren()) do
			v.Parent = game.Players.LocalPlayer.Backpack
		end
		for i, v in pairs(script:GetChildren()) do
			if v:IsA("Sound") then
				for i = 1, 10 do
					v.Volume = v.Volume - 0.1
						wait()
				end
				
				v.Volume = 0
				v:Stop()
			end
		end
	end
	canScan = true
end)

1 answer

✓ Accepted answer

Math.random() is an algorithm. It's not actually random but it's pseudorandom. It uses a seed to generate random numbers. What you need to do is give it a seemingly random seed too.

Use math.randomseed(param), where param is the seed. os.time() or tick() will do as the parameter. Place this at the start of your script.

This discussion is preserved as it was. New answers, comments, and votes are closed.