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

How to play a sound when looking at an object?

How do I trigger a sound to play when a player is looking at an object?

2 comments

0

Please make effort to post your own code.

0

Preferably calculate with LockFirstPerson enabled; use a Raycast from the Character's head to the Cursor .Hit Vector3 (Position). Use RunService to perpetually track the Object of contact returned, and compare. Disconnect the signal listener after matched.

1 answer

Easy :D

local mousein = false
script.Parent.MouseHoverEnter:Connect(function()
	mousein = true
end)
script.Parent.MouseHoverLeave:Connect(function()
	mousein = false
end)

while wait() do
	if mousein == true then
		script.Parent.Parent.Sound.Playing = true
	else
		script.Parent.Parent.Sound.Playing = false
	end
end

the script will work if the cube structure looks like this: https://ctrlv.cz/LeIT

If I helped, please mark my answer as correct :)

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