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

How do I make this model make a sound upon touched?

Asked by 8 years ago
Edited 8 years ago

I am making diamonds to collect on my map! They regen and give points perfectly, but they do not make a sound when collected. When I try to put in a sound, the script breaks! I also noticed other scripts will have the sound looping so if they stay in the spot where it regenerates, it plays the sound constantly.

I would like it to play the sound only when the item is not transparent. That, or only allow the sound to function every 7 seconds (the time it regens) whatever is easier :)

Here is my script without trying to add in any sounds -

(Please let me know what and where I need to put it! Thank you so much I appreciate it a ton!)

01waittime = 7 -- Time Between each hit
02amnt = 10 --how much you get for it
03function onTouched(part)
04    local h = part.Parent:findFirstChild("Humanoid")
05    if (h~=nil) then
06        local thisplr = game.Players:findFirstChild(h.Parent.Name)
07        if (thisplr~=nil) then
08            local stats = thisplr:findFirstChild("leaderstats")
09            if (stats~=nil) then
10                local score = stats:findFirstChild("Money")
11                if (score~=nil) then
12                    score.Value = score.Value + amnt
13 
14                end
15            end
View all 28 lines...

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

What you need

You need to reference your sound object, then simply use the Play function when the Touched event fires.


Notes

  • Establish a debounce to prevent repeating sounds.

  • Set the 'Looped' property of the sound object to false if you still find your sound repeating.

  • The GetPlayerFromCharacter function can be used here to retrieve the player easily


Code

01local sound = script.Parent:FindFirstChild("Sound") --This is your sound
02local waittime = 7 -- Time Between each hit
03local amnt = 10 --how much you get for it
04local db = false --the debounce
05 
06function onTouched(part)
07    if not db then
08        db = true --Turn on debounce
09        local thisplr = game.Players:GetPlayerFromCharacter(part.Parent)
10        if thisplr then
11            local stats = thisplr:FindFirstChild("leaderstats")
12            if stats then
13                local score = stats:FindFirstChild("Money")
14                if score then
15                    score.Value = score.Value + amnt
View all 27 lines...
0
It worked, thank you so much! You are amazing! Thank you for explaining to me how it works as well! callmehbob 54 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

I can help you if you want but this is not a requesting website I will hand you a link if you have any troubles tell me here: http://wiki.roblox.com/index.php?title=API:Class/Sound

1
why did I get a down vote johndeer2233 439 — 8y
1
Fine next time I will just give the answer johndeer2233 439 — 8y

Answer this question