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

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

Asked by 9 years ago

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

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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.
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. Relatch 550 — 9y
0
There is no need to downvote me. Relatch 550 — 9y
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. Perci1 4988 — 9y
Ad
Log in to vote
-2
Answered by
Relatch 550 Moderation Voter
9 years ago
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)
0
Thanks! But what about the print? iiCasual 20 — 9y
0
I fixed it, sorry. Lol. Relatch 550 — 9y
0
When I click the part it won't work. Do I put it in the click detector? iiCasual 20 — 9y
0
You used the incorrect event. Perci1 4988 — 9y
0
dont only post code please explain your answers. ZeptixBlade 215 — 9y
Log in to vote
-2
Answered by 9 years ago

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)
0
You used the incorrect event. Perci1 4988 — 9y
0
Thanks for reminding me. fahmisack123 385 — 9y

Answer this question