I have a script that creates a cone vision field of view for each player where they cant see other plays unless they are in this cone, but I want it so when a player fires their gun they appear for a second even if not in this cone. I can’t for the life of me figure it out I’ve tried just left mouse button triggers and using remote events but nothing seems to work. The script is in starterplayerscripts btw
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local playerGui = player:WaitForChild("PlayerGui")
local startButton = playerGui:WaitForChild("StartGUI"):WaitForChild("TextButton")
local visibilityEnabled = false -- Flag to control visibility based on button click
-- Create the black fog part
local blackFog = Instance.new("Part")
blackFog.Size = Vector3.new(100, 100, 100)
blackFog.Transparency = 0.95
blackFog.Anchored = true
blackFog.CanCollide = false
blackFog.Color = Color3.new(0, 0, 0)
blackFog.Material = Enum.Material.SmoothPlastic
blackFog.Parent = workspace
-- Create the spotlight
local spotlight = Instance.new("SpotLight")
spotlight.Angle = 100 -- Adjust the cone angle as needed
spotlight.Brightness = 1
spotlight.Range = 90 -- Adjust the range as needed
spotlight.Face = Enum.NormalId.Front
spotlight.Parent = head
local visibilityParts = {
Visibility = workspace:WaitForChild("Visibility"),
Invisibility = workspace:WaitForChild("Invisibility")
}
local visibilityState = {} -- Keep track of each player's visibility state
-- Function to update the fog position
local function updateFogPosition()
blackFog.CFrame = CFrame.new(head.Position) * CFrame.Angles(0, math.rad(head.Orientation.Y), 0)
end
-- Function to check if a player is within the vision cone
local function isPlayerInVisionCone(targetPlayer)
local targetCharacter = targetPlayer.Character
if targetCharacter and targetCharacter:FindFirstChild("Head") then
local targetHead = targetCharacter.Head
local directionToTarget = (targetHead.Position - head.Position).unit
local headLookVector = head.CFrame.LookVector
local angle = math.acos(headLookVector:Dot(directionToTarget))
local withinCone = angle <= math.rad(spotlight.Angle / 2) and (targetHead.Position - head.Position).magnitude <= spotlight.Range
-- Check for obstructions using raycasting
if withinCone then
local rayOrigin = head.Position
local rayDirection = (targetHead.Position - head.Position).unit * (targetHead.Position - head.Position).magnitude
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character, targetCharacter}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult and raycastResult.Instance then
return false
end
return true
end
end
return false
end
-- Function to smoothly change transparency
local function smoothTransparency(part, targetTransparency, duration)
local initialTransparency = part.Transparency
local startTime = tick()
while tick() - startTime < duration do
local elapsedTime = tick() - startTime
local alpha = elapsedTime / duration
part.Transparency = initialTransparency + (targetTransparency - initialTransparency) * alpha
task.wait()
end
part.Transparency = targetTransparency
end
-- Function to update player visibility and highlight
local function updatePlayerVisibility()
for _, targetPlayer in pairs(game.Players:GetPlayers()) do
if targetPlayer ~= player then
local targetCharacter = targetPlayer.Character
if targetCharacter then
local isVisible = false
if visibilityEnabled then
isVisible = isPlayerInVisionCone(targetPlayer)
local state = visibilityState[targetPlayer]
if state == "visible" then
isVisible = true
elseif state == "invisible" then
isVisible = false
end
end
for _, part in pairs(targetCharacter:GetDescendants()) do
if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
if not part:GetAttribute("OriginalTransparency") then
part:SetAttribute("OriginalTransparency", part.Transparency)
end
local targetTransparency = isVisible and part:GetAttribute("OriginalTransparency") or 1
task.spawn(smoothTransparency, part, targetTransparency, 0.05) -- Adjust duration as needed
elseif part:IsA("Decal") and part.Name == "face" then
part.Transparency = isVisible and 0 or 1
elseif part.Name == "RankUI" then
part.Enabled = isVisible
end
end
end
else
if character:FindFirstChild("RankUI") then
character.RankUI.Enabled = true
end
end
end
-- Update visibility of blood parts
for _, bloodPart in pairs(workspace:GetDescendants()) do
if bloodPart:IsA("BasePart") and bloodPart:GetAttribute("IsBloodPart") then
local isVisible = false
if visibilityEnabled then
local directionToBlood = (bloodPart.Position - head.Position).unit
local headLookVector = head.CFrame.LookVector
local angle = math.acos(headLookVector:Dot(directionToBlood))
isVisible = angle <= math.rad(spotlight.Angle / 2) and (bloodPart.Position - head.Position).magnitude <= spotlight.Range
end
task.spawn(smoothTransparency, bloodPart, isVisible and 0 or 1, 0.05) -- Adjust duration as needed
end
end
end
-- Update the fog position and player visibility every frame
game:GetService("RunService").RenderStepped:Connect(function()
updateFogPosition()
updatePlayerVisibility()
end)
-- Ensure the fog updates with character movement
character.HumanoidRootPart:GetPropertyChangedSignal("CFrame"):Connect(updateFogPosition)
-- Handle new players joining
game.Players.PlayerAdded:Connect(function(newPlayer)
newPlayer.CharacterAdded:Connect(function()
updatePlayerVisibility()
end)
end)
-- Handle players leaving
game.Players.PlayerRemoving:Connect(function(removingPlayer)
visibilityState[removingPlayer] = nil
updatePlayerVisibility()
end)