Trying to make a spawning percentage system

Hello everyone ive finally finalized my spawning system but now i want a percentage system in there. ive tried to use math.random. (ill show an example soon) but that as well didnt work. Everytime the number ive set gets chosen the math.random line doesnt work anymore it keeps picking the number ive picked for some reason.

heres my code.

local RS = game:GetService("ReplicatedStorage")
local AppleTree = workspace:WaitForChild("AppleTree"):WaitForChild("Fruits")
local ApplesClone1 = RS:WaitForChild("AppleTreeFruits"):GetChildren()
local Players = game:GetService("Players")
local Lifetime = 80
local RandomAppleTimer = math.random(1, 2)

local Min = 1
local Max = 5
while true do
	Luck = math.random(Min, Max)
	print(Luck)
	wait(0)
	if Luck == 3 then
		print("Luck")
		local function SpawnApple()

			local function pickRandomApple(folder)
				local randomIndex = math.random(1, #folder)
				return folder[randomIndex]
			end

			local randomApple = pickRandomApple(ApplesClone1)

			print("spawning apple")

			local AppleClone = randomApple:Clone()

			AppleClone.Name = "Apple"
			AppleClone.Parent = AppleTree

			local Handle = AppleClone:FindFirstChild("Handle")
			if Handle then

				Handle.Touched:Connect(function(otherPart)
					local model = otherPart:FindFirstAncestorOfClass("Model")

					if model then
						local player = Players:GetPlayerFromCharacter(model)

						if player then
							Handle.Anchored = false
						end
					end
				end)

			end

			local function setApplePos(apple)

				local treePivot = AppleTree.Parent:GetPivot() -- this is a cframe value 
				local spawnBox = workspace.AppleTree:WaitForChild("SpawnBox1")

		--[[
			spawnbox is equal to a part in the workspace (or wheverer you want)
			this part defines the specific spawning area minus the y because you will set the part
			at the y-height you want to spawn the apples at

			this code gets the size of said spawning part and changes the position of the apple before
			it spawns to a random spot in the spawnPart size/2
		--]]

				local hitbox = spawnBox.Size
				local ranSpawnX = math.random(-hitbox.X/2, hitbox.X/2)
				local ranSpawnZ = math.random(-hitbox.Z/2, hitbox.Z/2)

				apple.Handle.Position = Vector3.new(treePivot.X + ranSpawnX, spawnBox.Position.Y, treePivot.Z + ranSpawnZ)
			end

			setApplePos(AppleClone)

			coroutine.wrap(function() -- puts the delete timer in a new thread so the for loop can run without yielding
				task.wait(Lifetime)

				if AppleClone.Parent == AppleTree then  -- checks if the apple has been picked up/doesnt exist in the tree anymore
					AppleClone:Destroy()
				end
			end)()
		end

		while true do
			if Luck == 3 then
				wait()
				SpawnApple()
			end
		end
	end

end


1 Like

You’re second while loop is running forever:

Random Luck number eventually gets chosen due to while loop → Checks if Luck is equal to the specified number → sets up the SpawnApple() function → Proceeds to start a while true do loop which checks if Luck is equal to the chosen number → The parent function already checked this, therefore you’re code is the same as while Luck == 3 do so it just runs the SpawnApple() function forever.

This should work, though I havent tested it. Instead it declares the function, sets a spawn rate which you can adjust as needed (the spawn_Rate variable) and then starts a while loop with a 1 second interval to spawn apples according the spawn rate.

local RS = game:GetService("ReplicatedStorage")
local AppleTree = workspace:WaitForChild("AppleTree"):WaitForChild("Fruits")
local ApplesClone1 = RS:WaitForChild("AppleTreeFruits"):GetChildren()
local Players = game:GetService("Players")
local Lifetime = 80
local RandomAppleTimer = math.random(1, 2)

local function SpawnApple()

	local function pickRandomApple(folder)
		local randomIndex = math.random(1, #folder)
		return folder[randomIndex]
	end

	local randomApple = pickRandomApple(ApplesClone1)

	print("spawning apple")

	local AppleClone = randomApple:Clone()

	AppleClone.Name = "Apple"
	AppleClone.Parent = AppleTree

	local Handle = AppleClone:FindFirstChild("Handle")
	if Handle then

		Handle.Touched:Connect(function(otherPart)
			local model = otherPart:FindFirstAncestorOfClass("Model")

			if model then
				local player = Players:GetPlayerFromCharacter(model)

				if player then
					Handle.Anchored = false
				end
			end
		end)

	end

	local function setApplePos(apple)

		local treePivot = AppleTree.Parent:GetPivot() -- this is a cframe value 
		local spawnBox = workspace.AppleTree:WaitForChild("SpawnBox1")

--[[
	spawnbox is equal to a part in the workspace (or wheverer you want)
	this part defines the specific spawning area minus the y because you will set the part
	at the y-height you want to spawn the apples at

	this code gets the size of said spawning part and changes the position of the apple before
	it spawns to a random spot in the spawnPart size/2
--]]

		local hitbox = spawnBox.Size
		local ranSpawnX = math.random(-hitbox.X/2, hitbox.X/2)
		local ranSpawnZ = math.random(-hitbox.Z/2, hitbox.Z/2)

		apple.Handle.Position = Vector3.new(treePivot.X + ranSpawnX, spawnBox.Position.Y, treePivot.Z + ranSpawnZ)
	end

	setApplePos(AppleClone)

	coroutine.wrap(function() -- puts the delete timer in a new thread so the for loop can run without yielding
		task.wait(Lifetime)

		if AppleClone.Parent == AppleTree then  -- checks if the apple has been picked up/doesnt exist in the tree anymore
			AppleClone:Destroy()
		end
	end)()
end

local spawn_Rate = 10 -- 10%
while true do 
	local luck = math.random(1,100)
	if luck <= spawn_Rate then
		SpawnApple()
	end
	task.wait(1)
end
1 Like

thank you so much, your explanation was very detailed i appreciate it

1 Like

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