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.
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.
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)
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)