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

how do I make a humanoid make some sound?

how do I make a humanoid make some sound (using Sound) if any player gets close? For example a scream

1 answer

Give each player a hitbox, then trigger the sound when someone comes into contact with that hitbox. To prevent spamming, you'll want a debounce in there too.

local scream = "rbxassetid://292124372"
game:GetService("Players").PlayerAdded:Connect(function (p)
	p.CharacterAdded:Connect(function (c)
		local sound = Instance.new("Sound", c.Head)
		sound.SoundId = scream
		local hitbox = Instance.new("Part", c)
		hitbox.Size = Vector3.new(20, 20, 20) -- you might want to fiddle with this a bit
		hitbox.CanCollide = false
		hitbox.Transparency = 1
		hitbox.Anchored = false
		local weld = Instance.new("Weld", c.HumanoidRootPart)
		weld.Part0 = c.HumanoidRootPart
		weld.Part1 = hitbox
		-- no need for C0 and C1
		local db = true
		hitbox.Touched:Connect(function (h)
			if db and game:GetService("Players"):GetPlayerFromCharacter(h.Parent) then
				db = false
				sound:Play()
				wait(2) -- you also might want to change this
				db = true
			end
		end)
	end)
end)

2 comments

1

where should this function be located? in which service?

0

ServerScriptService or workspace. I usually put scripts handling game mechanics in SSS, but you can put it in a server script wherever server scripts can be ran, since you're making a hitbox for each player that needs to replicate.

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