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

Help with a touch script? :)

I want someone to touch the 'trigger' then a new sound is made and put in the players StarterGui so it plays only for them, but I don't want the sound to play another time for the player,I only want it to play once then the brick can identify who has gone through and who hasn't so the sound can't play a second time! I would like to have a debounce in it aswell, but I don't understand how to do it? Here is what I have so far:

function onTouched(Part) 
	local SoundMaker = Instance.new("Sound", game.StarterGui) 
	SoundMaker.SoundId = ("http://www.roblox.com/asset/?id = 156637449")
	local Sound = game.StarterGui.Sound
	Sound:Play()
end

Much appreciated if anyone can help? :)

2 answers

✓ Accepted answer

Okay, there's many errors here. You can copy and paste the full script below, and you can remove the comments if you'd like:

db = false --Debounce variable. False means the function will run and true means the function won't run.

function onTouched(Part)
	if db then return end --If db is true, then stop the function.
	db = true --Set db to true so the function can't be repeated while it's true.
	if Part.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(Part.Parent) then --If there is a Humanoid in the model and a player is found then...
		local plr = game.Players:GetPlayerFromCharacter(Part.Parent) --The player which is found.
		if plr.PlayerGui:FindFirstChild("onTouchSound") then return end
		local sound = Instance.new("Sound", plr.PlayerGui) --Make the sound
		sound.Name = "onTouchSound"
		sound.SoundId = "http://www.roblox.com/asset/?id=156637449" --No need for spaces in the SoundId
		sound:Play() --Play the sound
	end
	wait(2) --Change to how long you want the debounce delay to be.
	db = false --Sets db to false so the function can run again.
end

script.Parent.Touched:connect(onTouched)

2 comments

0

16:44:48.336 - Workspace.Part.Script:9: ')' expected (to close '(' at line 1) near ''

0

Wait the sound is playing!

script.Parent.Touched:connect(function(hit)
    local SoundMaker = Instance.new("Sound", game.StarterGui) 
    SoundMaker.SoundId = ("http://www.roblox.com/asset/?id = 156637449")
    local Sound = game.StarterGui.Sound
    Sound:Play()
    Sound.Parent = hit.Parent
end

5 comments

0

Why thumbs down? I edited it.

1

I hear no sound. But thanks for helping!

0

No need to thumbs down twice. Jesus christ.

0

If the answer you've given isn't useful, people are going to down vote it.

0

This works, its just I want to know how to only make it play one time.

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