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

attempt to index nil with 'KeyDown' ?

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!

1 comment

0

it's a serverscript. that's why.

1 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.

2 comments

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.

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

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