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
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.
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.
Please use codeblocks: they allow for readability of your code.
Numbers don’t have a Changed event
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
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