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

Why isn't "Onclick" working? And why won't it print?

So, these are the scripts.

local s = Instance.new("Sound")

s.Name = "Sound"
s.SoundId = "http://www.roblox.com/asset/?id=130766856" --Audio id
s.Volume = 7.25
s.Looped = false
s.archivable = false

s.Parent = game.Workspace

wait(0)

Onclick = s:play() 

And the print one.

local Onclick print "Feed me, Feeeeeeed me"

Why won't the onclick work? I have a click detector.

3 answers

✓ Accepted answer

You can't just make up random pieces of text and assume they'll do what you want. They way you have it set up on line 13, Onclick is nothing more than a variable. There is nothing special about the words Onclick.

To make a part clickable, first you have to put a ClickDetector in it, then use the MouseClick event of the ClickDetector. An event is basically something that prevents a specific code block from running until its conditions are met. In this case, we want to prevent code from running until the MouseClick event is fired.

*I'll assume both the Script and the ClickDetector are direct children of the Part. *

To do this, we first put an area of code inside a function. We do this because functions do not run until they are called. Regular code blocks run as soon as the game starts, but functions don't run until we make them run.

function playSound()
	local s = Instance.new("Sound")
	s.Name = "Sound"
	s.SoundId = "http://www.roblox.com/asset/?id=130766856" 
	s.Volume = 7.25
	s.Looped = false
	s.Archivable = false --Make sure it's capitalized.
	s.Parent = game.Workspace
end

As is stands, this code will not run because we are not doing anything to make it run. This is good, because we want to make it so that this function will execute every time the MouseClick event is fired. We do this with what is commonly called a connection line, we must connect the function to the MouseClick event.

function playSound()
	local s = Instance.new("Sound")
	s.Name = "Sound"
	s.SoundId = "http://www.roblox.com/asset/?id=130766856" 
	s.Volume = 7.25
	s.Looped = false
	s.Archivable = false
	s.Parent = game.Workspace
end

script.Parent.ClickDetector.MouseClick:connect(playSound) --Connect the playSound function to the MouseClick event.

3 comments

0

How was I supposed to know he/she was using a ClickDetector? I answered this earlier when he/she didn't explain it was a ClickDetector. I thought it was a Gui.

0

There is no need to downvote me.

1

The person posted this an hour ago, you put your question 55 minutes ago. Please read the question throughly in the future, for the asker explains a ClickDetector is being used.

local s = Instance.new("Sound")

s.Name = "Sound"
s.SoundId = "http://www.roblox.com/asset/?id=130766856" --Audio id
s.Volume = 7.25
s.Looped = false
s.archivable = false

s.Parent = game.Workspace

wait(0)

script.Parent.MouseButton1Down:connect(function()
	s:Play()
	print("Feed me, feeeeeeed me")
end)

5 comments

0

Thanks! But what about the print?

0

I fixed it, sorry. Lol.

0

When I click the part it won't work. Do I put it in the click detector?

0

You used the incorrect event.

0

dont only post code please explain your answers.

Onclick means nothing yet. You need something called an event and a function.

example:

script.Parent.Clicked:connect(function(player) --"Clicked" is the event and player is the person who clicked it.
print("I GOT CLICKED!!!")
end)

2 comments

0

You used the incorrect event.

0

Thanks for reminding me.

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