Script that detects height

I’m working on an ocean-themed game, and I’m trying to create a script that locally makes the fog black when the player goes below a certain depth, and changes back when going above the same depth, but I can’t seem to get it to work.

The code is as follows:

while true do
local player = game.Players.LocalPlayer.Name
local char = game.Workspace:FindFirstChild(player)
local depth = char.HumanoidRootPart.Position.Y
if depth < -80 then
game.Lighting.FogColor = Color3.new(0, 0, 0)
else
game.Lighting.Fogcolor = Color3.new(45, 73, 76)
end

wait(0.1)

end

Any idea what’s wrong?

3 Likes

Is it giving any errors, what have you tried so far to fix the problem?

Side note: Use the Changed property of an object instead of the “while true” loop. It’s in general considered a bad practice.

It doesn’t seem to give any errors. I tried adding a print function within the if function, which didn’t activate at all, so it seems like it’s unable to detect the height at all. The localscript’s parent is Workspace if that matters btw.

LocalScripts don’t run inside Workspace, try placing it in StarterCharacterScripts (you can even use script.Parent for the character then)

2 Likes

As a side note, i would recommend using player.Character

local Character  = game.Players.LocalPlayer.player.Character

to get a player’s character instead of looking through the Workspace to find it.

1 Like

@ReturnedTrue already said that also you can’t use “Y” for changing a players y position you have to use Vector3 and do things from there.

Here is an article: Vector3 | Documentation - Roblox Creator Hub

1 Like

I’m not trying to alter the player’s Y position though, I’m only trying to detect it.

I see, I still believe it is more efficient to use Vector3 though.

I made some changes, and ended up with this:

local char = script.Parent.HumanoidRootPart
local y = char.Position.Y

y.Changed:Connect(function(NewY)
if y < -80 then
print(“Depth is below -80”)
game.Lighting.FogColor = Color3.new(0, 0, 0)
else
game.Lighting.Fogcolor = Color3.new(45, 73, 76)

end

end

)

It still doesn’t work, and it gives me the following error:
Workspace.battlecruiser12.FogChange:4: attempt to index local ‘y’ (a number value)

Like I said Vector3 is more efficient with the position, don’t just pull Y out. btw make sure to preformat your script, so it is easier to read

How do I get the Y value from a Vector3 though? I understand how to use Vector3.new, but I can’t figure out in which way to use Vector3.Y to extract the Y value.

You can use the Vector3.FromAxis Instance, also Changed can’t be used towards a Vector3. Changed is only meant for what the error said, number values, so like a type of currency one player might have.

All in the link I provided you: Vector3 | Documentation - Roblox Creator Hub

I have a few things to address:

  1. Please use codeblocks: they allow for readability of your code.
  2. Numbers don’t have a Changed event
  3. Color3.new only accepts values in the range of 0 - 1, because of that you can use Color3.fromRGB or divide each component by 255
  4. Use player.Character instead of indexing the player’s name in the workspace
    5.You’re better off with a Heartbeat event and obtaining the height and adjust the lighting accordingly
local RS =game:GetService("RunService") 
local char = script.Parent
local hrp = char.HumanoidRootPart

RS.Heartbeat:Connect(function() 
    if hrp.Position.Y < -80 then
         game.Lighting.FogColor = Color3.fromRGB() 
    else
          game.Lighting.FogColor = Color3.fromRGB(45, 73, 76)
    end
end) 

Also, please name your variables accordingly. Don’t use char for a variable name if it references their HumanoidRootPart, instead, name the variable hrp or something

4 Likes

Thank you! This works perfectly. I had no idea that Heartbeat events were a thing. I will definitely make use of that in the future.