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

What's wrong with this on click script? Did I forget something? (Simple)

(I am just now getting into scripting, so it might be messy or unusual)

I have a TextButton in a ScreenGui that is only visible to me. This script is inside of the TextButton. Basically, it creates a sound into my head only (I'm using R15) and plays it when the TextButton is clicked by me, and it isn't working.

sound = Instance.new("Sound")
sound.Parent = game.Workspace.SheriffTaco.Head
sound.SoundId = "rbxassetid://846221985"
sound.MaxDistance = 500
sound.RollOffMode = "Linear"
sound.Volume = 10
script.Parent.MouseButton1Click:connect(function()
  sound:Play()
end)

2 comments

0

The 2nd line (sound.Parent = game.Workspace.SheriffTaco.Head) won't work, I'm still learning too, but from my personal experience, you should set a variable for players plr = game.Players:Getplayers() first and then set the Parent thing. I might be wrong, but you should try it.

0

Okay, thanks

2 answers

✓ Accepted answer
local function PlaySound(ID,Parent)
    local Sound = Instance.new("Sound")
    Sound.SoundId = "rbxassetid://"..ID
    Sound.PlayOnRemove = true
    Sound.MaxDistance = 500
    Sound.RollOffMode = "Linear"
    Sound.Parent = Parent
    Sound.Volume = 10
    Sound:Destroy()
end

local c = game:GetService("Players"):GetPlayers()


script.Parent.MouseButton1Down:Connect(function()
for i = 1,#c do
	local Player = c[i]
	if Player.Name == "SheriffTaco" then
	PlaySound(846221985, Player.Character.Head)
	end
end
end)
	

3 comments

0

Oh wow I forgot a lot, thanks.

0

You shouldn't have changed MouseButton1Click to MouseButton1Down. The code will repeat until the mouse is no longer down.

0

Mb. I looked at the function incorrectly.

--I found out how to work this, Make sure you have the gui enabled for you on however you know for you to see it only, then make sure the parent of this script is a textbutton, and when the textbutton is clicked this would function, make sure you don't click it right away because theres the wait(2), try it out, it worked for me.

wait(2)
sound = Instance.new("Sound")
sound.Parent = game.workspace.SheriffTaco.Head
sound.SoundId = "rbxassetid://846221985"
sound.MaxDistance = 500
sound.RollOffMode = "Linear"
sound.Volume = 10
script.Parent.MouseButton1Click:connect(function()
	sound:Play()
end)

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