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

How do I make a morph that when a button is stood on it'll play a sound?

I'm trying to make a radio that morphs onto your back that plays a song when you morph, anyone does know the problem?

function onTouched(hit) 
	local d = hit.Parent:GetChildren() 
	for i=1, #d do 
		if (d[i].className == "Model") then 
script.Parent.Parent.Chest.Sound:Play()
wait(script.Parent.Parent.Chest.Sound)
end

Thanks if you can help, also I'm a bit of a noob, so go easy on me, please!

1 answer

You're missing an end, so we have a syntax error to begin with. You're waiting for an invalid time (the time is not a number, it's an instance). It's important that we don't assume that the part that touched was a player. Additionally, you have no event connection to that listener function. Let's fix this!

local isDebounced = false
local debounceTime = 2

local function onTouched(touchedPart)
	if isDebounced then
		return
	end

	isDebounced = true

	-- touchedPart.Parent may have been set to nil between when the event fired and when our event listener runs, so let's check for that.
	if not touchedPart.Parent then
		return
	end

	local player = game.Players:GetPlayerFromCharacter(touchedPart.Parent)

	if not player then
		return
	end

	-- Check if they've already been morphed here
	-- Attach radio to player's back here
	-- Play sound

	wait(debounceTime)
	isDebounced = false
end

script.Parent.Touched:Connect(onTouched)

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