Can someone help please???
Part = script.Parent
Sound = Part.Sound
function onClicked()
Sound:Play()
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Can someone help please???
Part = script.Parent
Sound = Part.Sound
function onClicked()
Sound:Play()
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
You named your script Sound, try to avoid duplicate names to avoid this issue.
The script is also named Sound
so it finds the script first and ends up trying to do :Play()
on the script.
Basically, because inside the Part there are two objects with the name sound, your script is identifying the script. Change the sound of either object.
dam i didnt noticed that lol thank you
local Part = script.Parent
local Sound = Part:WaitForChild("Sound")
function onClicked()
Sound:Play()
end
script.Parent:WaitForChild("ClickDetector").MouseClick:connect(onClicked)
What the previous posts suggested, rename the script to “Script” as it would be by default, additionally I’ve added a couple of waits (this is to allow for the requested instances to load before the script is executed), finally you should declare variables locally, here’s why:
Local variables are obtained faster than global variables because they’re integrated into the environment in which they were created. If possible, you should always use local variables over global variables, unless there’s a specific reason otherwise.