Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make it so that this sound only plays once?

I made a script that makes it so that when a player clicks on my selected model, it plays a sound How can i make it so that after a player clicks on the part once it does not play again when clicking on it a second time?

basically can i make it so that each player can play the sound once

This is the script I'm currently using:

Brick = script.Parent
Sound = Brick.Sound2

function onClicked()
Sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 comment

0

Are you wanting the player only to be able to play it once?

3 answers

✓ Accepted answer

You say per player, which means other players should still be able to click it, so here's what I think should work for your use.

Brick = script.Parent
Sound = Brick.Sound2
banned = {} -- Adds the ban list as a table so the script doesn't error.

function onClicked(plr)
for i,v in pairs(banned) do -- Checks the banned list for people not allowed to use the button.
	if v==plr.Name then 
		return
	end
end
Sound:Play()
table.insert(banned,#banned+1,plr.Name) -- Adds the player to the ban list
end

Brick.ClickDetector.MouseClick:connect(onClicked)

I tested this before I posted it and it worked so it should work on your game. All I did is add a list that keeps track of all of the players that have hit the button before and when someone clicks it goes through this list and checks to see if the player clicking is on the list, and if they are it won't play the sound, if they aren't it plays the sound and adds them to the list. If you want to reset the list just add a line "banned = {}" and the list will be cleared, and previously banned players will be able to press the button again.

1 comment

0

Thanks so much dude this was very helpful:)

function onClick()
    sound:Play()
    sound.Parent = nil
end

2 comments

0

That is very inefficient. It's much better if you just destroy the sound object entirely.

0

Either that, or if it is to be reused, Fifkee's suggestion of using the PlayOnRemove property of the sound is applicable.

You could enable PlayOnRemove and then remove the sound object when clicked on. If you plan to recycle the sound after a match ends or so, you could simply clone the sound back into the object.

1 comment

0

connect Is deprecated, consider using Connect.

This discussion is preserved as it was. New answers, comments, and votes are closed.