NumberRange Issues

I don’t know if I am overcomplicating this, but I can’t figure out how to check if an IntValue is between specific numbers to find a specific zone.

My module storing depth variables:

return {
	["Surface"] = 0,
	["Underground"] = -10,
	["The Void"] = -3000,
}

If the IntValue is at -472, it should return ‘Underground.’ Please help me with this!!!

(Note the IntValue is this formula:

Depth.Value = (math.floor((HumanoidRootPart.Position.Y / 5) * 10) / 10)
1 Like

This don’t make sense to me. What are you doing here? And how are we ending up with this…

return {
	["Surface"] = 0,
	["Underground"] = -10,
	["The Void"] = -3000,
}

Why is this a table?

How?

I’m listing the Zones in numerical order.

Heres an updated version.

return {
	["Surface"] = 0,
	["Underground"] = -10,
	["Traveling"] = -60,
	["Bodies"] = -120,
	["Deep"] = -200,
	["Unknown"] = -300,
	["Treacherous Halls"] = -400,
	["Uncharted Territory"] = -500,
	["The Void"] = -3000,
}

How would I make it like, if the player’s Depth is between -120 and -200, it would return Bodies?

Depth … meaning their Y position?

Depth is the formula I showed in the main post.

Ok, I figured that, just making sure. why is this going to a .Value?
And where are you reading that value to figure the return.

Elaborate please, and Depth is a NumberValue

Is there a reason you’re storing that, that way. And again I don’t see where you’re reading that value from it after you set it with Depth.Value = (math.floor((HumanoidRootPart.Position.Y / 5) * 10) / 10)

Oh, I haven’t done it yet, I still haven’t figured out how to do it.

Can we just do this?

--local script
local player=game:GetService("Players").LocalPlayer
local humanoidRootPart=player.Character:WaitForChild("HumanoidRootPart")
local depth=nil

function getZone()
	depth=(math.floor((humanoidRootPart.Position.Y/5)*10)/10)
	if depth >= 0 then return "Surface" end
	if depth >= -10 then return "Underground" end
	if depth >= -60 then return "Traveling" end
	if depth >= -120 then return "Bodies" end
	if depth >= -200 then return "Deep" end
	if depth >= -300 then return "Unknown" end
	if depth >= -400 then return "Treacherous Halls" end
	if depth >= -500 then return "Uncharted Territory" end
	if depth >= -3000 then return "The Void" end
	return "Unknown"
end

local zone=getZone()

print(zone)

But that would be inefficient…

Working is better than inefficient looking. Also that is tight for what it is doing. I don’t know where you’re using this so it’s a bit of guessing. The function part is all that matters. Server or local Client script … once you lock onto HumanoidRootPart.

Alright, I guess I can use it. I will test it later though.

If you want, I can show you my client and server scripts.

That is a given when asking for help … or you leave us guessing

local Zones = {
  {["Name"] = "Surface"; ["Depth"] = 0};
  {["Name"] = "Underground"; ["Depth"] = -10};
  {["Name"] = "Traveling"; ["Depth"] = -60};
  ...
}

function getZone()
  Depth = (math.floor((humanoidRootPart.Position.Y / 5) * 10) / 10)
  for _, Zone in Zones do
    if Depth < Zone.Depth then continue end
    return Zone.Name
  end
  return "Unknown"
end
-- To match the module:
function getZone()
  Depth = (math.floor((humanoidRootPart.Position.Y / 5) * 10) / 10)
  for ZoneName, ZoneDepth in Zones do
    if Depth < ZoneDepth  then continue end
    return ZoneName
  end
  return "Unknown"
end

Client Script:

--// Variables
local Player = game.Players.LocalPlayer

local Zones = require(game.ReplicatedStorage:WaitForChild("Zones"))

while wait() do
	
	script.beep:Play()
	script.Parent.Value.Text = Player.Depth.Value .. " m"
	
	task.wait(2)
	
end

Server Script:

--// Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

Players.PlayerAdded:Connect(function(player: Player)
	
	local Depth = Instance.new("NumberValue")
	Depth.Parent = player
	Depth.Name = "Depth"
	
	RunService.Heartbeat:Connect(function()
		
		if player.Character then
			
			local Root = player.Character:FindFirstChild("HumanoidRootPart")
			if Root then
				
				Depth.Value = (math.floor((Root.Position.Y / 5) * 10) / 10)
				
			end
			
		end
		
	end)
	
end)

Again, why use a table for this…

if i wanted to add a new zone, i’d rather modify a table than add another if statement

1 Like