Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Play music when one player sees another player?

|| Preface: Sorry my english. I use Google translate ||

How can I play music for a player when he sees another? I will be happy all the details

1 answer

✓ Accepted answer

Use math.acos lookVector:Dot() .unit Camera.FieldOfView and maybe RayCasting

I'm not sure about your level on scripting but I'll make a basic example (LocalScript, because Camera):

local cam = workspace.CurrentCamera
local parttocheck = workspace.part

function CheckVisibility(part)
	local angle = math.acos(cam.CoordinateFrame.lookVector:Dot((part.Position - cam.CoordinateFrame.p).unit)) -- returns angle in rad between the part and your camera's lookVector unit (small vector)
	
	if angle < math.rad(cam.FieldOfView) then -- if the angle is smaller than the angle of your camera, it's obviously IN your camera :)
		return true
	else
		return false
	end
end

print(CheckVisibility(parttocheck))

with vertical visibility it might not work properly

What I mentioned the RayCasting for: the function works if the part is in the angle of your FieldOfView, so it will also return true if a part is in front of you but BEHIND other parts.

What you want to do now is loop it on all players' HumanoidRootParts and play the sound if it returns true

This discussion is preserved as it was. New answers, comments, and votes are closed.