Spawnlocations that enables and disables depending on RNG

I’m trying to make a game where every time you respawn or join, a script runs which makes spawn locations enable and/or disable themselves. For example, I want one Spawnlocation to be enabled with a 1 in 2 chance, while the other spawnpoints are disabled. I’m not that experienced in scripting. Is there a way to achieve this?

2 Likes

Let’s say you have a world with 10 spawnpoints.
You want the game to pick 1 of those locations for a player to spawn in, which changes every time they respawn?

Yeah, but they would have varied chances. For instance, one spawnpoint has a 1 in 2 chance, while another has a 1 in 4 chance, and only one spawnpoint would be active at a time.

I was make to get this but idk what u mean by

local spawns = workspace.Spawns

for _,v in spawns:GetChildren() do
v.Disabled = true
end

local lucky = spawns:GetChildren()[math.random(1,#spawns:GetChildren())]
lucky.Disabled = false

Will give you a prototype soon.
Precise’s code snippet is on the right track. You can play around with it in the meantime.

1 Like

This hopefully is a good prototype for RNG spawning.

Add a NumberValue inside of each location, and set the value to the % you’d want, so for example 1 out of 5 would be 20% (20 as NumberValue), and 1 out of 1000 would be 0.1% (0.1 as NumberValue).
image

--Function to apply RNG
local function chooseRandomSpawn(tableOfSpawns)
	
	local scaleFactor = 10000 --This allows very low chances to be added, such as 1 in a million
	local totalChance = 0

	for location,chance in pairs(tableOfSpawns) do

		totalChance += chance * scaleFactor

	end

	local randomNumber = math.random(0,totalChance - 1)
	local cumulativeChance = 0

	for location,chance in pairs(tableOfSpawns) do

		cumulativeChance += chance * scaleFactor

		if randomNumber < cumulativeChance then

			return location

		end

	end
	
end

--Define spawnLocationsFolder
local spawnLocationsFolder = game.Workspace:WaitForChild("SpawnLocations")

--Define table to keep track of chances
local tableOfSpawns = {}

--Loop to fill table
for index,spawnLocation in pairs(spawnLocationsFolder:GetChildren()) do
	
	--Disable previous spawns
	spawnLocation.Enabled = false
	
	--Define chance for the spawn location in that iteration
	local chanceOfSpawn = spawnLocation:WaitForChild("ChanceOfSpawn")
	
	--Fill table
	tableOfSpawns[spawnLocation] = chanceOfSpawn.Value
	
end

--Apply RNG when character spawns
game.Players.PlayerAdded:Connect(function(plr)

	--Define location to enable
	local chosenLocation = chooseRandomSpawn(tableOfSpawns)

	--Enable chosen location
	print(chosenLocation)
	chosenLocation.Enabled = true
	
	plr.CharacterAdded:Connect(function(character)
		
		--Set up a died listener
		local humanoid = character:WaitForChild("Humanoid")
		
		humanoid.Died:Connect(function()
			
			--Disable previous spawns
			for index,spawnLocation in pairs(spawnLocationsFolder:GetChildren()) do
				
				spawnLocation.Enabled = false
				
			end

			--Define location to enable
			local chosenLocation = chooseRandomSpawn(tableOfSpawns)

			--Enable chosen location
			print(chosenLocation)
			chosenLocation.Enabled = true
			
		end)
		
	end)
	
end)
1 Like

now you will need some extra work for making this multiplayer friendly. The way I would do it is to create a value inside of each player, and then change that value, instead of the actual spanwLocation’s enabled property. Then teleport the player to the spawn location that matches the player’s value. I can make this tommorow if you have trouble with it.

Or you can just fire a RemotEevent to the client if you don’t mind potential cheating of the spawn system.

Yes, that would be pretty easy to do… however, you need to show us where you are and post your current script. We can help you fix a script. We are not to wright you a script from scratch.
Failing until you get it right is very much a part of learning to program.

I’m having trouble putting the percentage of 1 in 69 million in the number value, as it is a ridiculously small decimal. How can I make this work, and what does the totalChance variable do?

Every decmial value of a chance is written as 1 / chance.
So the number value of 1 in 69 million is 1 / 69000000. You can use a calculator to easily see the number.
You will also need to add 0 twice on the scale factor so it can handle up to 1 in 100 million. The scale starts at 100, so the factor needs to be multiplied by 1 million (1000000) to reach 100 million.

The total chance is just to make sure that when you add values that stack above 100%, the program doesn’t bug.