Sound doesn't play

  1. What do you want to achieve? Sound plays when holding the mouse.

  2. What is the issue? Script, line 2. Doesnt work.

  3. What solutions have you tried so far? Yes, I did but there was no solution I see.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local sound = script.Parent.Sound
local holding = false

Mouse.Button1Down:Connect(function()
	holding = true
end)

Mouse.Button1Up:Connect(function()
	holding = false
end)

--More script Below (In game)

You can’t access the LocalPlayer through a ServerScript, set its RunContext to Client.

1 Like

Still doesn’t play it. Any idea?
Full srcipt

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local sound = script.Parent.Sound
local holding = false

Mouse.Button1Down:Connect(function()
	holding = true
end)

Mouse.Button1Up:Connect(function()
	holding = false
end)

while true do
	if holding == true then
		sound:Play()
		task.wait(sound.TimeLength)
	else
		break
	end
end

Explorer
image

You should use UserInputService instead.

local UserInputService = game:GetService("UserInputService")
local sound = script.Parent.Sound
local holding = false

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = true
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = false
	end
end)

while holding do
	sound:Play()
	task.wait(sound.TimeLength)
end
1 Like

Still doesn’t. Is this script really connected to ui button?

Wait, I think this should work.

local UserInputService = game:GetService("UserInputService")
local sound = script.Parent.Sound
local holding = false

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = true
		while holding do
			sound:Play()
			task.wait(sound.TimeLength)
			if not holding then break end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = false
	end
end)

I did it.

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = false
		sound:Stop()
	end
end)

Added. sound:Stop()

Thank you.

1 Like

I actually need help, the sound plays when i press anywhere. So I wanna make only when pressed the button.

local button = path.Button
local sound = script.Parent.Sound
local holding = false

button.MouseButton1Click:Connect(function()
	holding = true
	while holding do
		sound:Play()
		task.wait(sound.TimeLength)
		if not holding then break end
	end
end)

button.MouseButton1Up:Connect(function()
	holding = false
	sound:Stop()
end)

I tried that by myself as soon as i got error. But doesn’t work.

Can you show the error? ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

Unfortunately, there is no error in output. No solution?

local button = path.Button
local sound = script.Parent.Sound
sound.Looped = true
local holding = false

button.MouseButton1Click:Connect(function()
	holding = true
	sound:Play()
end)

button.MouseButton1Up:Connect(function()
	holding = false
	sound:Stop()
end)

No, still doesn’t work. UserInputService can’t use it. Any other solution?

Solved. Final Script

local button = script.Parent
local sound = script.Parent.Sound
local holding = false

button.MouseButton1Down:Connect(function()
	holding = true
	sound:Play()
end)

button.MouseButton1Up:Connect(function()
	holding = false
	sound:Stop()
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.