Why Wont My Box's Spawn?

Hello Devs! I am making a game about going around and collecting CryoBoxes. There are different types of Cryoboxes, and I made it so that it gets a random number and then spawns a certain box depending on the numbers. They are boxes that give you something called scrap, and sometimes give you artifacts which you can sell to make profit. I want to make them spawn a respawn points I put around the map. But they won’t appear and I don’t think I’m getting any errors.

Some Pictures Of My Explorer:
image

My Script:

--This is the Script in the SpawnLocations folder
local NormalVal = math.random(1,69)
local RareVal = math.random(70, 90)
local UltraVal = math.random(91, 100)

while wait(1) do
	for k, n in pairs(script.Parent:GetChildren()) do
		if n.Name == "Location" then
			local CryoboxNumber = math.random(1, 100)

			if CryoboxNumber == NormalVal then --Normal CryoBoxes
				local NormalBox = game.ReplicatedStorage.CryoBox:Clone()
				NormalBox.Part.CFrame = n.CFrame
			end

			if CryoboxNumber == RareVal then --Rare Cryoboxes
				local RareBox = game.ReplicatedStorage.CryoBox:Clone()
				RareBox.Part.CFrame = n.CFrame
			end

			if CryoboxNumber == UltraVal then --Ultra Cryoboxes
				local UltraBox = game.ReplicatedStorage.CryoBox:Clone()
				UltraBox.Part.CFrame = n.CFrame
			end
		end
	end
end
2 Likes

Try putting it in server storage and redoing the script to extract it from seerverstorage instead.

1 Like

This is assuming you wanted the boxes to be server sided.

1 Like

Try to make workspace their parent. For example:
RareBox.Part.Parent = workspace

2 Likes

Because your top variables are generating a single random number that is being compared to another single random number.

1 Like

How would I change this? Do you mind writing an example script?

It’s because you assign a random value for every rarity level at the top, (e.g. NormalVal could be let’s say 54) and then you compare the single value you just assigned to each rarity level to the random value you create every 1 second.

So unless the random “CryoboxNumber” in this case isn’t exactly 54 which is the NormalVal in this case it won’t match.

Updated script:

--This is the Script in the SpawnLocations folder
local NormalVal = NumberRange.new(1,69)
local RareVal = NumberRange.new(70, 90)
local UltraVal = NumberRange.new(91, 100)

local function isInRange(range, num)
	return num > range.Min and num < range.Max
end

while wait(1) do
	for k, n in pairs(script.Parent:GetChildren()) do
		if n.Name == "Location" then
			local CryoboxNumber = math.random(1, 100)

			if isInRange(NormalVal, CryoboxNumber) then --Normal CryoBoxes
				local NormalBox = game.ReplicatedStorage.CryoBox:Clone()
				NormalBox.Part.CFrame = n.CFrame
			end

			if isInRange(RareVal, CryoboxNumber) then --Rare Cryoboxes
				local RareBox = game.ReplicatedStorage.CryoBox:Clone()
				RareBox.Part.CFrame = n.CFrame
			end

			if isInRange(UltraVal, CryoboxNumber) then --Ultra Cryoboxes
				local UltraBox = game.ReplicatedStorage.CryoBox:Clone()
				UltraBox.Part.CFrame = n.CFrame
			end
		end
	end
end
1 Like

There’s nothing wrong with the code/logic itself, albeit we could make a better probability system then generating random numbers. The primary issue, however, is that when you clone the CryoBox and set its CFrame, you do not parent it to the workspace. As a result, the CryoBox exists only in memory and does not appear in the game world.

For a more sophisticated probability system, you might consider using weights and organizing values into a table like this:

local CryoBoxData = {
    { type = "Normal", weight = 60, model = game.ReplicatedStorage.CryoBox },
    { type = "Rare", weight = 30, model = game.ReplicatedStorage.RareCryoBox },
    { type = "Ultra", weight = 10, model = game.ReplicatedStorage.UltraCryoBox }
}

That’s beside the point however, the primary issue remains the lack of parenting, which needs to be addressed to ensure the CryoBox appears in the game world.

Alright I will try this out soon! Thank you!

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