so basically everytime you touch a part that in the script, it will play the music, but me wasn’t smart enough to know that Touched event will allways run and it will make the song repeat the first second xd
so i need help, i want the most efficient way to make song plays depends on where you are
If you want to continue using parts to trigger the music, you can add a variable called “playingSound”. Logic similar to this should be what you want for each sound part:
local playingSound = nil
game.Workspace.TemSound.Touched:Connect(function()
if playingSound and playingSound.Name ~= "Tem_Shop" then
playingSound:Stop()
playingSound = nil
end
if not playingSound then
playingSound = TemShopSound(Wherever this is stored)
playingSound:Play()
game.Workspace.TemSound.TouchEnded:Connect(function()
if playingSound and playingSound.Name == "Tem_Shop" then
playingSound:Stop()
playingSound = nil
end
end)
end
end)
Edit: Because you are using a module to handling playing the music, you can just store the name of the currently playing music. The logic is the same but you will just make different calls to play the music.
it doesn’t repeat the first second of the song , but it is kind of buggy sometimes if you leave the shop and enter back fast it plays the ruins song inside the shop for some reason, or if you jump in the shop it just stops playing sound
Something I’ve been doing lately for this kind of scenario is checking the players location every 0.1 seconds.
Let’s say the shop starts at -10, 0, -10 and ends at 10, 10, 10 (XYZ)
local playerLocation --Player's location
local shopSound --Shop sound
while wait(.1) do
if playerLocation.X <= 10 and playerLocation.X >= -10 and playerLocation.Y <= 10 and playerLocation.Y >= 0 and playerLocation.Z <= 10 and playerLocation.Z >= -10 then
if shopSound.IsPlaying == false then
shopSound:Play()
end
else
if shopSound.IsPlaying == true then
shopSound:Stop()
end
end
end
Might not look as pretty, but it gets the job done
This is a good method as it doesn’t rely on the physics engine and can be controlled more predictively. You can also probably get away with a longer wait as well.