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!
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.
--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