im trying to write a script that lowers a players visibility through fog and clocktime as soon as they reach a certain distance from spawn, but it never seems to work no matter how i rewrite it and no error comes out.
what am i doing wrong?
local Spawn = game.Workspace.SpawnLocation
local HumanoidRootPart = game.Players.LocalPlayer.Character.HumanoidRootPart
local Distance = (Spawn.Position - HumanoidRootPart.Position).Magnitude
local Lighting = game.Lighting
local Heartbeat = game:GetService(“RunService”).Heartbeat
Heartbeat:Connect(function()
if Distance > 100 then
if Lighting.ClockTime > 0 and Lighting.FogColor > Color3.fromRGB(0,0,0) and Lighting.FogEnd > 0 then
Lighting.ClockTime -= 0.1
Lighting.FogColor /= 10
Lighting.FogEnd -= 10
end
else
Lighting.ClockTime = 14
Lighting.FogColor = Color3.fromRGB(192,192,192)
Lighting.FogEnd = 1000
end
end)
First of all, please always format ur code using `````` like this:
local Spawn = game.Workspace.SpawnLocation
local HumanoidRootPart = game.Players.LocalPlayer.Character.HumanoidRootPart
local Distance = (Spawn.Position - HumanoidRootPart.Position).Magnitude
local Lighting = game.Lighting
local Heartbeat = game:GetService(“RunService”).Heartbeat
Heartbeat:Connect(function()
if Distance > 100 then
if Lighting.ClockTime > 0 and Lighting.FogColor > Color3.fromRGB(0,0,0) and Lighting.FogEnd > 0 then
Lighting.ClockTime -= 0.1
Lighting.FogColor /= 10
Lighting.FogEnd -= 10
end
else
Lighting.ClockTime = 14
Lighting.FogColor = Color3.fromRGB(192,192,192)
Lighting.FogEnd = 1000
end
end)
Secondly, always tell us what script that is and where it is!
And now my suggestion:
local player = game.Players.LocalPlayer
local character = (player.Character and player.Character) or nil
player.CharacterAdded:Connect(function(newChar)
character = newChar
end)
local Spawn = game.Workspace.SpawnLocation
local Lighting = game.Lighting
game.RunService.RenderStepped:Connect(function()
local Distance = (Spawn.Position - character.HumanoidRootPart.Position).Magnitude
if Distance > 100 then
if Lighting.ClockTime > 0 and Lighting.FogColor > Color3.fromRGB(0,0,0) and Lighting.FogEnd > 0 then
Lighting.ClockTime -= 0.1
Lighting.FogColor /= 10
Lighting.FogEnd -= 10
end
else
Lighting.ClockTime = 14
Lighting.FogColor = Color3.fromRGB(192,192,192)
Lighting.FogEnd = 1000
end
end)
You have to recalc the distance each frame - or else it won’t be update!
You also should use RenderStepped instead of Heartbeat - Heartbeat runs on physics frames and RenderStepped runs every frame!
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")
local localPlayer = Players.LocalPlayer
local spawnPart = workspace.SpawnLocation
RunService.RenderStepped:Connect(function()
local character = localPlayer.Character
if character then
local rootPart = character:FindFirstChild("HumanoidRootPart")
local origin = spawnPart.Position
local distance = (rootPart.Position - origin).Magnitude
if distance > 100 then
Lighting.ClockTime -= 0.1
Lighting.FogColor = Color3.fromRGB(0,0,0)
Lighting.FogEnd -= 10
else
Lighting.ClockTime = 14
Lighting.FogColor = Color3.fromRGB(192,192,192)
Lighting.FogEnd = 1000
end
end
end)
local Spawnloc = game.Workspace.SpawnLocation
local HumanoidRootPart = game.Players.LocalPlayer.Character.HumanoidRootPart
local Lighting = game.Lighting
local Heartbeat = game:GetService("RunService").Heartbeat
while task.wait() do
local Distance = (Spawnloc.Position - HumanoidRootPart.Position).Magnitude
if Distance > 100 then
Lighting.ClockTime -= 0.1
Lighting.FogColor = Color3.new(0, 0, 0)
Lighting.FogEnd -= 10
else
Lighting.ClockTime = 14
Lighting.FogColor = Color3.fromRGB(192,192,192)
Lighting.FogEnd = 1000
end
end