i tried to make a script where when a player press a button the sound will pause, and if the button is clicked again it will resume but i keep getting the same error message
here’s what i tried so far
local button = script.Parent
local GUI = script.Parent.Parent
local Frame = GUI.Scrolling
local lights = game.Lighting.ColorCorrection
local sound = game.Workspace:IsA("Sound")
Frame.Visible = false
local clicked = false
button.MouseButton1Click:Connect(function()
if clicked == false then
sound:Pause()
lights.Saturation = -1
Frame.Visible = true
wait()
clicked = true
end
button.MouseButton1Click:Connect(function()
if clicked == true then
sound:Resume()
lights.Saturation = 0
Frame.Visible = false
wait()
clicked = false
end
end)
end)
the button is actually a settings button, so the player can change the music without me writing the name each time. is there a other/better way to do it?
First understand what you are doing here
as you are saying:
button.MouseButton1Click
you have two functions to connect the event with which is breaking the whole concept.
Instead you can try doing
button.MouseButton1Click:Connect(function()
if clicked == false then
– do what you want
else
– do what you want
end
end)
Secondly as @RocketB0ii has mentioned you are trying to check if workspace is a sound?
workspace is a service and to go more over these basic in-built functions I suggest you go over the dev hub and there are alot of more sources as well.
local button = script.Parent
local GUI = script.Parent.Parent
local Frame = GUI.Scrolling
local lights = game.Lighting.ColorCorrection
local sound = game.Workspace:FindFirstChild("Sound name here")
Frame.Visible = false
button.MouseButton1Click:Connect(function()
if sound.Playing == true then
sound:Pause()
lights.Saturation = -1
Frame.Visible = true
else
sound:Resume()
lights.Saturation = 0
Frame.Visible = false
end
end)
There is no need to change the name of the actual sound object, if a player wants to change the song all you need to do is change the SoundId property of the sound object.