I put this in scripting support since I’m guessing this requires scripting, but I want to make an explosion sound different based on distance, but I can’t anything on it on the forum or on youtube.
Regular
Distant
If this is coming out of a part, then in a localScript you could use .Magnitude to get the distance between that part and the humanoidRootpart. Then you could have an if statement that would change the sound to distant if the magnitude is greater than say, 250 studs.
local explosionPart = game.workspace.Part --your explosion part
local player = game.Players.LocalPlayer
local root = player.Character:WaitForChild("HumanoidRootPart")
if (explosionPart.Position - root.Position).Magnitude > 250 then
explosionPart.sound.SoundId = --sound id here
end
You could just make it so this is a function called by a remoteEvent from the explosion that has the explosionpart itself as the parameter.
Just for context, magnitude just gets the distance between the two positions that isn’t from a single axis
Where do I put it exactly?
Script:
Workspace Layout:
Video:
Is the part in replicatedStorage? If not, I would recommend putting it there and cloning it. Just need to know before I can clarify what I meant before.
No, its not, its in workspace.
Would you mind providing the script for the explosion?
It’s actually just a flipbook sheet in particles
Oh, I see. In this case, you need to make it so whenever the sound plays. It triggers a remoteEvent in replicatedStorage. Then, inside of StarterPlayerScripts, put a local script that looks like this:
local event = game.ReplicatedStorage.event --your remote event
local root = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local function explosionSound(part)
if (part.Position - root.Position).Magnitude > 20 then
part.Sound.SoundId = --sound id here
end
end
event.OnClientEvent:Connect(function(explosionPart) explosionSound(explosionPart) end)
This would change the sound for only one client so it would work with multiple players.
For the serverSide, add a script inside of the explosionpart that looks something like this:
local event = game.ReplicatedStorage.event --your remote event
local function explode() --call this whenever the explosion sound is played
--do whatever for the explosion here
event:FireAllClients(script.Parent)
end
--you could basically set a while loop here that waits the amount
--of time before the explosion goes off and then calls the event
while true do
task.wait(explosionTime)
explode()
end
Sorry if there are typos, but this should be about what you want. If you have any questions or errors I would be happy to adjust the code for you.
Isn’t the part supposed to added as a variable in the local script version because it’s not working for me.
Sorry, the issue is with getting the rootPart as the character isn’t loaded in, try getting the rootPart as a variable inside the function instead of before. You could also change the variable to game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild(“HumanoidRootPart”)
Like this?
local event = game.ReplicatedStorage.RemoteEvent
local function explosionSound(part)
local root = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
if (part.Position - root.Position).Magnitude > 20 then
part.Sound.SoundId = "rbxassetid://7423534808"
end
end
event.OnClientEvent:Connect(function(explosionPart) explosionSound(explosionPart) end)
Yes I believe you could do it like that, but just to make sure there are no errors, try doing this for the variable:
local root = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("HumanoidRootPart")
Does this look correct because because it still isn’t working.
Script:
local event = game.ReplicatedStorage.RemoteEvent
local function explosionSound(part)
local root = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("HumanoidRootPart")
if (part.Position - root.Position).Magnitude > 20 then
part.Sound.SoundId = "rbxassetid://7423534808"
end
end
event.OnClientEvent:Connect(function(explosionPart) explosionSound(explosionPart) end)
Layout:
Can you try checking the output tab and seeing if there is an error? To turn it on go to view and then click output. If there is an error, then please send a screenshot of it here.
Edit: one thing I could think of is if you are playing the sound before you fire the event, so I would recommend actually playing the sound client side or just making sure it is run after the clientside script does.
Ok, I would try a few test with the print() method to make sure stuff is happening in order. Also, it could be helpful If I see the full server-side script. And if I haven’t already, the full client-side one.
What is the explosionTime because its getting the red line, do I need a number value for this?
Ok, I’ve gotten it working in a test game. Here is the server script (inside the part):
local event = game.ReplicatedStorage.RemoteEvent
local function explode()
event:FireAllClients(script.Parent)
end
while true do
task.wait(5)
explode()
end
and here is the client script (starterPlayerScripts)
local event = game.ReplicatedStorage.RemoteEvent
local function explode(part)
local root = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("HumanoidRootPart")
if (root.Position - part.Position).Magnitude > 20 then
part.Sound.SoundId = "rbxassetid://7423534808"
else
part.Sound.SoundId = "rbxassetid://4418405082"
part.Sound.Playing = true
end
event.OnClientEvent:Connect(function(part) explode(part) end)
the task.wait in the server script can be adjusted in order to fit when you want to play your sound.
To explain what is happening. The serverscript calls the function explode every 5 seconds. When it does this, it then fires a remoteEvent that sends a message to the client script with the part as a parameter. Once on the client side, it will adjust the sound if needed and then play the sound client side aswell, so make sure playing and looped are both off on the sound.
And to answer your question, yes the explosionTime is just supposed to be whatever your delayTime on the explosion is.
Once again, if there are any errors or questions have any questions, I am happy to help.
Got this in the output: Players.Lil_devboy123.PlayerScripts.LocalScript:12: Expected ‘end’ (to close ‘function’ at line 3), got
Sorry, mistake on my end, try this:
local event = game.ReplicatedStorage.RemoteEvent
local function explode(part)
local root = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("HumanoidRootPart")
if (root.Position - part.Position).Magnitude > 20 then
part.Sound.SoundId = "rbxassetid://7423534808"
else
part.Sound.SoundId = "rbxassetid://4418405082"
end
part.Sound.Playing = true
end
event.OnClientEvent:Connect(function(part) explode(part) end)
Edit: I have tested this one more time, and I would recommend chaning the distance to more than 20, something more like the original 250 gives the right effect, but not necessary of course.