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

I'm trying to make a 'play sound on touched' script. Can you help me?

Btw, this is my first script I've done completely from scratch so it isn't very good.. :/

-- Playsound on touched script -- CanPlay = false script.parent.CanTouch = true if script.parent.touched then PartTouched = true

if PartTouched = true then CanPlay = true 
	if CanPlay = true then Play local sound
		wait (5)
		CanPlay = false
		if CanPlay = false then stopsound
	end
	LocalSoundId = rbxassetid://6490981787
end

2 answers

✓ Accepted answer

It appears as if you're new to scripting, so here is a list of improvements to your code.

  • When calling functions (like Play and stopsound as in your example) you need to use parenthesis (which are these: ()) in order for the compiler to read them as function calls. Using only Play and stopsound will make the compiler think you're referring to the variables associated with the functions, not the function call itself. As an append to the previous statement, if you do not have these functions defined, you can define them using the function command:
function Play(args) -- args include any arguments you may want to pass
	-- What you want to accomplish with this function goes here.
end

function stopsound(args)
	-- Same with this function.
end
  • When verifying that two values are equal to one another, use two equal signs instead of one. One equal sign specifies assignment in Lua; two specify the conditional syntax of equal to. For example:
a = 5
b = 10
if a == b then -- Will never pass because a < b
	print(a + b)
end
-- However, if we divide b by 2...
b = b / 2
if a == b then -- This should pass now!
	print(a + b)
end
  • Make sure that all of your functions, loops, and statements have ends. Roblox normally does this for you, but it's good practice in case IntelliSense (the UI and auto-filler of the script editor) doesn't pick up on it. I mention this because your first and third if statements have no ends tied to them.

  • The format rbxassetid://ID, with ID being the desired audio ID in your case, is a Content object and must be specified as a string. This means LocalSoundId should be set to "rbxassetid://6490981787", not just rbxassetid://6490981787.

  • Referring to your commented section, don't try using script signals as booleans. They should be treated as signals; in order to link to a signal, use Connect() from the signal. Also, use CanTouch.Value instead of CanTouch because you're trying to set a userdata value to a boolean.

When scripting, remember that practice makes perfect. If you want to get better, absorb the necessary information and practice.

1 comment

0

Thankyou for your help :)

local audiolength = 1 -- set audio length

local function playSound(part)
	local s = Instance.new("Sound") -- new sound
	s.SoundId = "rbxassetid://6490981787"
	s.Volume = 10 -- set volume
	s.Parent = part -- sets parent to part
	s:Play() -- plays it
	game.Debris:AddItem(s,audiolength) -- adds sound for audiolength time
end

local debounce = false -- debounce

script.Parent.Touched:Connect(function() -- connects to function on touched
	if debounce then return end --  when debounce is true it wont play
	debounce = true -- debounce set true
	playSound(script.Parent) -- play the function above
	wait(audiolength) -- wait till audio is done
	debounce = false -- debounce false
end)

1 comment

1

Thankyou for your help. :)

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