Both sounds play at the same time

I’ve been recently making a script where if a proximity prompt is triggered then a sound will be played. So I have 2 sounds in my soundService and in the script I load them with the :PreloadAsync() function.

Here’s the script:

local SoundService = game:GetService("SoundService")
local ContentProvider = game:GetService("ContentProvider")
local Sound1 = SoundService:WaitForChild("Sound1")
local Sound2 = SoundService:WaitForChild("Sound2")
ContentProvider:PreloadAsync({Sound1})
ContentProvider:PreloadAsync({Sound2})

local parent = script.Parent.Parent.Parent.Parent

script.Parent.Triggered:Connect(function()
	for index, value in pairs(parent:GetChildren()) do
		if value.Color == Color3.fromRGB(255, 227, 84) and value.SurfaceGui.TextLabel.Text ~= parent.SurfaceGui.TextLabel.Text then
			Sound1:Play()
		elseif value.Color == Color3.fromRGB(255, 227, 84) and value.SurfaceGui.TextLabel.Text == parent.SurfaceGui.TextLabel.Text then
			Sound2:Play()
		end
	end
end)

Don’t mind the mess of the script, all that you need to know is when a proximity prompt is triggered it checks all the parts in a model and then if a part is the color == Color3.fromRGB(255, 227, 84) and the part’s textLabel number does not equal to the floor that player is currently at, then sound 1 will be played, but the problem is both of the them play, even though I specified just one of them. But when it gets to the elseif statement where if the color is Color3.fromRGB(255, 227, 84) but the player is at the floor number equal with the number textLabel of that color part it should play a different sound but in this case it doesn’t play anything. What is the issue?

Can’t you just replace the elseif with else?

It wouldn’t matter I will get the same result.

Can I see it in the explorer so I can see the layout?

I will record a video and show you what I mean.

robloxapp-20230508-1300196.wmv (3.4 MB)

First sound:

Second sound:

And if you listen closely you can hear that both of the play at the same time.

What Color is 255, 227, 84 is it white?

Yellow, but I don’t think it matters.

Is there only the 3 buttons in the parent:GetChildren()?

Yes, as I have shown in the video.

The proximity prompt might be firing twice

Finally, I have figured out the issue, it was the fault of the :PreloadAsync() function. As you can see in the script when I implement them, it sets them in a multiple table thing. However I don’t how to explain this thing. But the thing is that I haven’t implemented the :PreloadAsync()function correctly, that’s all.

I deleted that 2 lines of code and everything works perfectly.

1 Like

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