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

attempt to index nil with 'KeyDown' ?

Asked by 4 years ago

Hello, i have this script that spawns a trap, but it dosent spawn in? Can someone help?

local plr = script.Parent.Parent.Parent.Parent
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(Key)
    local k = Key:lower()
    if k == "CTRL" then
        if k == true then
            local trap = game.ServerStorage.Trap:Clone()
            trap.Parent = workspace
            trap.Position = plr.Character.LeftFoot
            plr.Character.Humanoid.Jump = true
        end
    end
end)

Thanks!

0
it's a serverscript. that's why. Fifkee 2017 — 4y

1 answer

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

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Roblox does not recommend using mouse.KeyDown as it is deprecated. Use UserInputService instead. Make sure it is a localscript.

local plr = script.Parent.Parent.Parent.Parent --So many parents
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, GPE) --Input is self-explanatory, GPE stands for Game Processed Event.
    if GPE then return end --If the player is e.g. chatting, then do nothing.
    if input.KeyCode == Enum.KeyCode.LeftControl then
        local trap  = game.ReplicatedStorage.Trap:Clone() --Serverstorage can only be accessed by server scripts.
        trap.Parent = workspace
        trap.Position = plr.Character.LeftFoot
        plr.Character.Humanoid.Jump = true
    end
end)

If it doesn't work, then you should use remote events instead. Edit: bruh I posted an answer, and I also used comments to explain stuff.

0
He didn't write the code posted in his question, it's from a free model. The current state of the Mouse object on the docs doesn't even show KeyDown anymore. SteamG00B 1633 — 4y
0
Why would you use a free model? Just learn the thing by yourself, and if there is a problem, then just post a question. @Johannes_11 Dovydas1118 1495 — 4y
Ad

Answer this question