Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
3wdo 198
5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)


0
it says: unexpected symbol near ')' 3wdo 198 — 5y
0
on what line, you could give more info before downvoting me FallenZalio 99 — 5y
0
line 13 3wdo 198 — 5y
0
btw i dont have enough reputation to downvote 3wdo 198 — 5y
0
now try it, edited FallenZalio 99 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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

0
its just resets the music 3wdo 198 — 5y

Answer this question