i added music in my simulator game and i want to make a button to make the music stop and start if the press it again. but it says: attempt to index local 'input' (a boolean value)
my code:
game:GetService("UserInputService").InputBegan:Connect(function(player, input) if input.KeyCode == Enum.KeyCode.m then game.Workspace.Sound:Stop() end end) game:GetService("UserInputService").InputBegan:Connect(function(player, input) if input.KeyCode == Enum.KeyCode.m then game.Workspace.Sound:Play() end end)
Just use a debounce, that way you don't have to make multiple functions for one action.
local muted = false --- assuming they start with the music playing game:GetService("UserInputService").InputBegan:Connect(function(player, input) if muted == false then muted = true if input.KeyCode == Enum.KeyCode.m then game.Workspace.Sound:Stop() else muted = false if input.KeyCode == Enum.KeyCode.m then game.Workspace.Sound:Play() end end end end)
Input began doesnt have a player parameter, its correct representation is
UIS.InputBegan:Connect(function(input,gpe) end)
where Input
is an InputObject and GPE
is a boolean that in most cases is used to see if the player is typing while the event fires.Although it technically Indicates whether the game engine internally observed this input and acted on it.
so for your case, a corrected version would look like this
local playing = false UIS.InputBegan:Connect(function(input,gpe) if gpe then return end if input.KeyCode == Enum.KeyCode.M then if playing then sound:Stop() else sound:Play() end playing = not playing end end)
if you wanted to pause rather than stop the sound, substitute stop for pause