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

Problem with KeyDown event on my code?

Asked by
R_alatch 394 Moderation Voter
8 years ago

So, basically I have this code that is trying to give a player a ball when "b" is pressed on their keyboard. This works in studio, but when tried in-game I get an error saying "attempt to index global 'mouse' a nil value". Any ideas?

game.Players.PlayerAdded:connect(function(player)
    local mouse = player:GetMouse()
    mouse.KeyDown:connect(function(key)
        if key == "b" then
            game.ReplicatedStorage.Ball:Clone().Parent = player.Backpack
        end
    end)
end)
1
KeyDown is deprecated, you should use UserInputService or ContextActionService instead. SynphonyKnight 85 — 8y

1 answer

Log in to vote
2
Answered by
rexbit 707 Moderation Voter
8 years ago

:GetMouse() can only be accessed by a LocalScript, attempting to use a script will error.

local Mouse = Game.Players.LocalPlayer:GetMouse()

Mouse.KeyDown:connect(function(k)
    if key == "b" then
        game.ReplicatedStorage.Ball:Clone().Parent = player.Backpack
    end
end)
0
Check the capitalization of Mouse. XAXA 1569 — 8y
Ad

Answer this question