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

Music that plays when near a player on a certain team?

Asked by 2 years ago

I'm creating an asymmetrical horror game very similar to Dead by Daylight, and I need help on a script that creates music that plays when near (Around 35 studs) a player on the Killer team. (I use teams to distinguish between Survivors and Killers.)

I'm pretty new to scripting, so help would be appreciated!

0
Oh yeah, I would prefer it if only the survivors could hear the music and not the killer team. SanicBoy727 2 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Hey there! I would start by inserting a 'Sound' into part of the killer's character. In this instance, I am going to insert it into the head. That can be done with a simple line of code:

--You will need to make a variable called "killer" in order for this to work. If you need help with this, let me know.

local sound = Instance.new("Sound")
sound.Parent = killer.Head
sound.SoundId = 12345678

There is a feature that should be built into the Sound called RollOffMaxDistance. This controls how many studs away a player can hear that sound. You can read about it here: https://developer.roblox.com/en-us/api-reference/property/Sound/RollOffMaxDistance

Then, to make only survivors hear the sound, you will want to use a LocalScript. You will want to write something similar to this:

local killer = "none"
local players = game.Players:GetChildren()
for i = 1, #players do
    if players[i].Team == "Killer" then --Change this to your team name
        killer = players[i].Name
    end
end
local player = game.Players.LocalPlayer
local killerChar = game.Workspace:FindFirstChild(killer)
local sound = killerChar.Head:WaitForChild("killerSound")

if player.Team == "Killers" then
    sound.Volume = 0
end
if player.Team == "Survivors" then
    sound.Volume = 0.5
end

I hope this helps! Don't hesitate to ask if you have any questions! ~Amanda314159

EDIT: No worries! To make the killer variable, you will want this in your main script somewhere:

--Choose killer
    local players = game.Players:GetChildren()
    local killer = players[math.random(1,#players)]
    local killerChar = game.Workspace:WaitForChild(killer.Name)
    print(killer.Name.. " is the killer!")

    --put a sound into ReplicatedStorage, and name it killerSound. This will be the killer sound.
    local sound = game.ReplicatedStorage:WaitForChild("killerSound"):Clone()
    sound.Parent = killerChar:WaitForChild("Head")

--Fire remote event

Where it says fire remote event, you will want to fire a remote event to activate a localscript containing the second chunk of code that I wrote.

0
Thank you! You explained everything very clear, but something I'm having trouble with is calling the player on the killer side. (Creating the killer variable.) I'm sorry if I'm bothering you! SanicBoy727 2 — 2y
0
I tried using I, V pairs, but it just doesn't seem to work. SanicBoy727 2 — 2y
0
I edited my answer, let me know if that helps things. Here is more about remote events: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events Amanda314159 291 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
--This is in a normal script within ServerScriptService
--It doesn't play any sound
for i,v in pairs(game.Teams["Killer"]:GetPlayers()) do
    local sound = Instance.new("Sound")
    sound.Parent = v.Character.Head
    sound.SoundId = "http://www.roblox.com/Asset/?id=1167182574"
    sound.RollOffMaxDistance = 60
    sound.Volume = 1
    sound:Play()
end

Answer this question