So I wanna create an invisible part, that turns the player’s fog more thickened, however I want it to only be done via client side so only the person that touches that part is affected, and then if you reset, the fog is set back to normal until you touch that part again, how would this be possible? If someone could go into detail on how it’s done, id appreciate!
Just use local script to add fog, use lighting in-built fog properties and change them
You need a LocalScript or Script with RunContext set to Enum.RunContext.Client
to run code on the client.
To run code when the part is Touched, you need to ‘Connect’ a function to the BasePart.Touched
event. This will trigger on any collision with the part, so you need to filter it to just the LocalPlayer, using something like Players:GetPlayerFromCharacter()
on the Parent of the touching part (the character if the part is a body part), inversely you can use Humanoid.Touched
to detect when the character touches a part, useful if you want to connect to lots of parts to avoid duplicated code.
To detect when the character has respawned, you connect to the Player.CharacterAdded
event.
And finally for fog, you can use the Fog properties of the Lighting service, or for more realistic fog, an Atmosphere Instance parented to Lighting.
Tie them all together:
-- Services --
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")
local LocalPlayer = Players.LocalPlayer
local Part = script.Parent -- The part that you want to connect .Touched to
Part.Touched:Connect(function(otherPart: BasePart)
local character = otherPart.Parent
-- Check if otherPart's parent is a character of the local player
if character:IsA("Model") and Players:GetPlayerFromCharacter(character) == LocalPlayer then
-- And change the fog here!
end
end)
LocalPlayer.CharacterAdded:Connect(function(character: Model)
-- Character respawned, reset the fog!
end)
Try This Local Script:
local PartName = "<Your Part Name In Workspace>"
local Player = game.Players.LocalPlayer
local Tween
if Part:IsA("Part") then
Part.Touched:Connect(function(hit)
if hit.Parent == Player.Character then
if Tween then
Tween:Cancel()
end
Tween = game:GetService("TweenService"):Create(
game.Lighting,
TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),
{FogEnd = 248, FogStart = 24, FogColor = Color3.fromRGB(34, 34, 34)}
)
Tween:Play()
end
end)
elseif PartName and workspace:FindFirstChild(PartName) then
workspace:FindFirstChild(PartName).Touched:Connect(function(hit)
if hit.Parent == Player.Character then
if Tween then
Tween:Cancel()
end
Tween = game:GetService("TweenService"):Create(
game.Lighting,
TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),
{FogEnd = 248, FogStart = 24, FogColor = Color3.fromRGB(34, 34, 34)}
)
Tween:Play()
end
end)
end