How would I change the audio based on distance to the source

The part is 20 studs, I don’t know if its on my end or what, but it still isn’t working

Not sure what would be causing the issue as it works for me, it might just take a bit more walking if you haven’t already done that, try printing the magnitude as well just to see if the code is breaking or if you just aren’t far enough:

print((root.Position - part.Position).Magnitude)
1 Like

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
	print((root.Position - part.Position).Magnitude)
end

event.OnClientEvent:Connect(function(part) explode(part) end)

This is just to troubleshoot, but yes that should be right. Also make sure nothing else is changing the soundId of the part or playing it anywhere else.

Edit: thought I already sent this, but make sure your hierarchy looks similar to this:

1 Like

Wait, I need a script in the part and starterplayerscripts?

Yes, the part that I said was serversided should be inside the script within the explosionpart and the other code that says client side should be inside the localscript in starterplayerscripts. Sorry for the confusion.

1 Like

It works now, thank you for helping me!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.