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

I see, is everything working though or is there another issue?

not sure if I can add the sound to the parented script. If so, where can I add it?

You can add the sound inside of the script and in the while look, just replace script.Parent.Sound with script.Sound

that could be my issue, ill try that. Thanks for your help

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

2 Likes

here is my script now:


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)

while true do
	script.Sound1:Play()
	wait(200)
end

and here is my error output
12:05:11.746 Sound1 is not a valid member of Script “ServerScriptService.cmss” - Server - cmss:34

Line 34 thats its referencing is the script.Sound1:Play()

1 Like

Show the explorer, I think you may’ve wrote the name of the Sound incorrectly or something else

I created it as Sound1 in my explorer

1 Like

Did I name it properly in the script?

That’s why it ain’t working, it should be workspace.Sound1, not `script.Sound11, use the latter if you decide to put the sound in the script

oh…smh…let me try that then. So I should use:
game:workspace.Sound1:Play()
?

1 Like

trying this game me output error:

12:13:15.429 ServerScriptService.cmss:34: Expected ‘(’, ‘{’ or when parsing function call, got ‘.’ - Studio - cmss:34

1 Like
while true do
	game:workspace.Sound1:Play()
	wait(200)
end
1 Like

You should use workspace.Sound1:Play(). Even game.Workspace.Sound1:Play() will work but the former is more recommended since it’s more of a direct call to workspace without using game, although both practically do the same thing anyways

1 Like

that did the trick, thanks a ton!

So what I learned from all this is to run the while loop in the script and if the sound file is in the workplace, just eliminate the game: and use worksplace.sound:play()

thanks, I was really confused

Again, both game.Workspace and workspace will work since they both call the Workspace instance anyways. while true do loops have their own use cases, make sure you think through whe nconsidering a while true do and think if it’s the best to do it. And if it is, make sure you put a wait in the loop or else it will crash the script.

If you have anymore issues don’t be afraid to make another post!

1 Like

last question, is this sound audible to whole server or just local player?

If the sound was played in a regular script then it’s audible to the entire server, but if it was played in a local script, it will only be audible to you

1 Like

thanks again for everything. I appreciate your time and help

1 Like

Anytime! That’s what I’m here for, to help people like you! If you have anymore issues don’t be afraid to make another post!

2 Likes