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

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

Asked by 5 years ago

(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)
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. ArcanZus 61 — 5y
0
Okay, thanks SheriffTaco 13 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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)

0
Oh wow I forgot a lot, thanks. SheriffTaco 13 — 5y
0
You shouldn't have changed MouseButton1Click to MouseButton1Down. The code will repeat until the mouse is no longer down. User#19524 175 — 5y
0
Mb. I looked at the function incorrectly. FireyMcBlox 134 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

Answer this question