Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
yayachik1 -11
5 years ago

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)
0
Are you wanting the player only to be able to play it once? spartanlord456 19 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Thanks so much dude this was very helpful:) yayachik1 -11 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
function onClick()
    sound:Play()
    sound.Parent = nil
end
0
That is very inefficient. It's much better if you just destroy the sound object entirely. T0XN 276 — 5y
0
Either that, or if it is to be reused, Fifkee's suggestion of using the PlayOnRemove property of the sound is applicable. T0XN 276 — 5y
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago

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.

0
connect Is deprecated, consider using Connect. User#22722 20 — 5y

Answer this question