How do i make a sound loop when toggled?

what is that supposed to mean?

2 Likes

Like I wrote it, so for example:
button.MouseButton1Click:Connect(function()
script
end)

2 Likes

By hierarchy I meant basically your explorer Window

2 Likes

sending image:

1 Like

Oh I found the bug, your variable is spelled sound but in the function the S is capital

My bad, typo!

2 Likes

Hmm. I cant seem to put Connect in it. Im trying this btw:

local Sound = script:WaitForChild("trr")
local button = game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		input.KeyCode == Enum.KeyCode.Z
			local Activated = false

		end
		button:Connect(function()
		if Activated == false and Sound.Playing == false then
			Sound:Play
			Sound.Looped = true

			Activated = true

		elseif Activated == true and Sound.Playing == true then
			Sound:Destroy()
			Sound.Looped = false

			Activated = false

		end)
end)

i fixed the capital

1 Like
  1. Activated variable is inside of uis function
  2. The button:Connect isnt enough, as you need to specify when the function connects
1 Like

How do i specify when the function connects?

2 Likes

Like for example, if you write what needs to happen for the function to connect, so for example button.MouseButton1Click:Connect(function()ā€¦end)

Here we see that the function fires once the button has been clicked

1 Like

Its supposed to toggle when you press z

1 Like

Yeah then you need to replace button.MouseButton1Click with your keycode

1 Like

wait i got lost. what is the button variable supposed to be then?

1 Like

It was just a placeholder for your key

1 Like

what is my key? Also sorry for being so clueless, im a beginner. I hope it does not bother you.

1 Like

No it doesnā€™t bother me, trust me, Iā€™ve been there a couple months ago.
With the key, i mean your Z key (the one you want to be pressed for the sound to play!)

1 Like

Btw I can send you a full script later when I have time, cuz right now I have to do homework and I see our conversation getting longer and longer.

1 Like

k. thanks for the help though!

1 Like

Hello, sorry for the late reply, I will be able to provide you with a script in about half an hour

1 Like

hey there, Iā€™ve been trying to get this to work for the last 30 minutes, unfortunately it still doesnt. I changed the code a whole lot, this is what I ended up with:

local UIS = game:GetService("UserInputService")
local key = "Z"

local Sound = script:WaitForChild("Sound")
local Activated = false


UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode[key] then
        
        if Activated == false and Sound.Playing == false then
            Sound:Play()
            Sound.Looped = true

            Activated = true

        elseif Activated == true and Sound.Playing == true then
            Sound:Destroy()
            Sound.Looped = false

            Activated = false
        end
    end
end)

Again, Iā€™m sorry for wasting your time. If you have any ideas how to make it work, feel free to reach out to me and I will happily try to help you once again!

Best regards,
AlexPalex178

2 Likes

Sounds have to be parented to a Part or Attachment to play from just that Part and you can change the distance itā€™ll be heard from.
If the sound is in the Workspace (anyhwhere not in a Part or Attachment itā€™ll play everywhere in the game. I havenā€™t tried putting it in a script though.

1 Like