Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

line 8 "attempt to index local 'input' (a boolean value)" how do you fix this? [ANSWERED]

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)

2 answers

✓ Accepted answer

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)


5 comments

0

it says: unexpected symbol near ')'

0

on what line, you could give more info before downvoting me

0

line 13

0

btw i dont have enough reputation to downvote

0

now try it, edited

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

edited for toggling if the sound is playing

1 comment

0

its just resets the music

This discussion is preserved as it was. New answers, comments, and votes are closed.