how do I make a humanoid make some sound (using Sound) if any player gets close? For example a scream
COMMUNITY QUESTION
how do I make a humanoid make some sound?
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)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
where should this function be located? in which service?
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.