Help with getting the correct value

I don’t know what it’s called but this is what I want:

I want to spawn a castle from your level.
I don’t know what to do if your level does not match any of these:
image

local function spawnCastle(player, value)
	local castle = CastlesFolder:FindFirstChild(tostring(value))
	if castle then
		local newCastle = castle:Clone()
		newCastle.Name = player.Name.." Castle"
		newCastle.Parent = Castles
	end
end

What I want to happen:
If you are in Level 10 and the next castle is Level 15, then it would return the previous castle.

local function spawnCastle(player, value)
    -- Check if current level is at least 15
    if value >= 15 then
        local castle = CastlesFolder:FindFirstChild(tostring(value))
        if castle then
            local newCastle = castle:Clone()
            newCastle.Name = player.Name.." Castle"
            newCastle.Parent = Castles
        end
    else
        -- If current level is less than 15, return previous castle
        local castle = CastlesFolder:FindFirstChild(tostring(value - 5))
        if castle then
            local newCastle = castle:Clone()
            newCastle.Name = player.Name.." Castle"
            newCastle.Parent = Castles
        end
    end
end

I don’t see anything in your function that checks the players level.

If you have it somewhere then compare their level of the level they are asking for:

if currentLevel < than askingLevel then give them currentLevel

How would I get the next castle?

You line local function spawnCastle(player, value) is providing the askingLevel right?

local function spawnCastle(player, value) <---- value is the player level
	local castle = CastlesFolder:FindFirstChild(tostring(value))
	if castle then
		local newCastle = castle:Clone()
		newCastle.Name = player.Name.." Castle"
		newCastle.Parent = Castles
	end
end
local function spawnCastle(player, value)
    -- Check if next level is at least 15
    if value + 5 >= 15 then
        local castle = CastlesFolder:FindFirstChild(tostring(value + 5))
        if castle then
            local newCastle = castle:Clone()
            newCastle.Name = player.Name.." Castle"
            newCastle.Parent = Castles
        end
    else
        -- If next level is less than 15, return current castle
        local castle = CastlesFolder:FindFirstChild(tostring(value))
        if castle then
            local newCastle = castle:Clone()
            newCastle.Name = player.Name.." Castle"
            newCastle.Parent = Castles
        end
    end
end

But what if I don’t want to do it manually?
image

The code appears to be a Lua script that defines a function called “spawnCastle”. It takes two arguments, “player” and “value”, and checks if the next level is at least 15. If it is, it clones the castle model corresponding to the next level and renames it with the player’s name. Otherwise, it clones the castle model corresponding to the current level and renames it with the player’s name. It then sets the parent of the new castle model to the “Castles” object.

How would I do it without manually checking the players level?

Would I list all the castles in a table and get it from there?