How to play audio every x amount of minutes when wearing accessory

Hi, I have made a clown mask and want to know how I can script it to play audio file every x amount of minutes.

I have made an accessory and mask is wearable, i just need to be able to play an audio file ID every so often while the mask is being worn. How can I script this?

I think what you can do is insert a script that has a loop that repeatedly waits a few minutes and then play the audio. Make the script disable Then, in another script, probably in the Character which you can add via a script in StarterCharacterScripts, you can use the ChildAdded event on the Character to detect if something got, added, and if it was the mask accessory, enable the script. That’s atleast how I would do it when it’s worn

I think I have an idea to do that. I can do something like this:

while true do
game.Workspace.Sound:Play()
wait(200)

does that look about right? I am new to scripting

I would recommend putting the sound in the mask itself and doing this

while true do
   script.Parent.Sound:Play()
   wait(200)
end
1 Like

thank you, last newbie question, where do I assign the actual audio ID ?

In the sound element.


(Paste the ID of the audio in SoundId)

1 Like

You assign in the Sound itself via the Explorer. Get the id you want to use, click on the Sound i nthe explorer, go into its property and click on SoundId, paste the ID and press enter

2 Likes

where in the branch of my accessory do i place the script? handle parent? mesh?
I have it on serverscriptservice

You can put anywhere in the script, I would recommend putting the script nad the sound as parents of the Accessory instance

Wouldn’t wait(200) sometimes be over 200 because of server lag?

1 Like

I’m not sure, but I don’t think it should be anything to worry about for @pilers22, but if they want, they could research and find a Custom Wait which I recall was in the DevForum

Why use a while true do loop? Just have the sound parented to the mask, Looped is on and Playing is true. This will work just fine :confused:

Not using a while true do wouldn’t work in this case since OP specifies he wants it to play a sound after 200 seconds (it could be a simple sound effect like those Accessories that make a sound). If we used Looped and Playing, it will repeatedly play the sound even if 200 seconds haven’t passed since it will the sound again when the audio has finished. This is the simplest way to achieve what he wanted, a sound that plays every 200 seconds

1 Like

Ah, my bad. I figured it was something like music that plays. I didn’t read the part about “every 200 seconds” correctly I guess :sweat_smile:

1 Like

Haha, that’s okay! Sometimes we skim through what we’re reading and forget to notice the important details. But yea, your idea would work better if it was an Accessory that played music, but he never really specified, but if made my answer as the solution, then it was most definitely just a sound effect

1 Like

exactly. I was unable to place the script in the mask accessory, as it did not work. But i ended up putting it in workspace. Maybe that would be funnier for what i am doing…i have 2 sounds, one is a background music which i renamed to BG and my sound1 (while looped) i couldnt figure out why sound isnt playing with the mask

I see, did you manage to get all working in the end? I’m not sure why the script wasn’t working in the Accessory, quite odd

i have several scripts on the last. It is supposed to be for game pass, whoever has the mask it will make a sound. It just wouldnt play. I put it “script” under the handle, and tried it as a parent of the accessory (which there already is a script) I also tried adding it the scripts to no avail

By “Already is a script” was it one of yours or did it come with the mask?

here is my explorer window

here is my script in the explorer window:

local MarketplaceService = game:GetService('MarketplaceService')

local GAMEPASSID = 15193367

local function AddHatToCharacter(Character)
	-- Run code to add hat.
	script:FindFirstChildOfClass('Accessory'):Clone().Parent = Character
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, GamePassId, Purchased)
	if Purchased then -- They actually bought it, and didn't click 'Cancel'
		if GamePassId == GAMEPASSID then -- It's the hat gamepass
			Player.CharacterAdded:Connect(function(Character)
				AddHatToCharacter(Character)
			end)

			if Player.Character then
				AddHatToCharacter(Player.Character)
			end
		end
	end
end)

game:GetService('Players').PlayerAdded:Connect(function(Player)
	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GAMEPASSID) then
		Player.CharacterAdded:Connect(function(Character)
			AddHatToCharacter(Character)
		end)
	end
end)