Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do I make a humanoid make some sound?

Asked by
lytew 99
4 years ago

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

1 answer

Log in to vote
1
Answered by
compUcomp 417 Moderation Voter
4 years ago

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)
1
where should this function be located? in which service? lytew 99 — 4y
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. compUcomp 417 — 4y
Ad

Answer this question