How do i make a sound loop when toggled?

Before. I am a beginner, so i will need more detailed explanations. So. My goal here is to make a sound loop when toggled. And when you toggle it off, it stops. Any help? Thanks.
Bob is a remoteevent.

This is my code so far:
(this is a localscript in startergui)

local uis = game:GetService("UserInputService")
local key = {
	Z = false
}
uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if input == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Z then
			key = true
		end
	end
end)
game:GetService("RunService").RenderStepped:Connect(function()
	if key.Z then
		game.Workspace.bob:FireServer()
		end
		end)

And the Bob script:

script.Parent.OnServerEvent:Connect(function()
	if game.Workspace.fulram.Sound.Playing == true then
		game.Workspace.fulram.Sound.Ended:Wait()
		game.Workspace.fulram.Sound:Play()
	end
end)

1 Like

local sound = your sound here
local button = the thing you need to press for the sound to play
local Activated = false

button.MouseButton1Click: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)

1 Like

Is this in a localscript? Or a remoyteeverbt?

1 Like

You can probably put this in a local script, with the sound as the child of the script

1 Like

Whenever the script mentions “sound” it says “Unknown global “sound””

1 Like

Did you set your variable to your sound?

1 Like

yes it is indeed changed and i am actually a water bottle

2 Likes

What? I don’t understand what you’re saying.

1 Like

i wrote the confusing thing because i need to write 3o character

2 Likes

Ah ok, what does it say in the output know? Or still the same thing?

1 Like

still, unknown global thingy. it has not changed

1 Like

Can you show me the script inside studio? My script’s lines weren’t formatted when I wrote it

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

		end
		button
		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)
1 Like

trr is also the sound that is used

1 Like

And button is “Incomplete statement: expected assignment or a function call”

1 Like

Try using WaitForChild instead of script.trr, maybe the script is mistaking this line for a script’s property instead of child

1 Like

Yeah you didn’t wrap the function , you just wrote button

1 Like

Still a unknown global

unknown global is still therfe

1 Like

How do i wrap the function?
How do i wrap the functiion?

1 Like

Hmm, can you show me your hierarchy?

1 Like