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

ServerScriptService.Script:3: attempt to index nil with 'IsKeyDown' ?

I encountered a problem in my script when i was checking if a player was holding down the Q key have no idea how i can fix this.

Here are my two scripts :

The Client script

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local Mouse = player:GetMouse()

local character = player.Character or player.CharacterAdded:Wait()

local block = game.Workspace.Part:Clone()
block.Parent = workspace

local Key = 'Q'

local debounce = true

UserInputService.InputBegan:Connect(function(Input ,IsTyping)
	if IsTyping then return end
	local KeyPressed = Input.KeyCode
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		if KeyPressed == Enum.KeyCode[Key]and debounce and character then
			game.ReplicatedStorage.FireBallEvent:FireServer(Mouse.Hit, UserInputService, Key)
		end
	end
end)

The Server Script

local fireball = Instance.new("Part", workspace)

game.ReplicatedStorage.FireBallEvent.OnServerEvent:Connect(function(player, mouse, UserInputService, Key)
	while wait(1) do 
		if UserInputService:IsKeyDown(Enum.KeyCode[Key]) then
			print("the fireball is being charged!")
			fireball.Size = fireball.Size + Vector3.new(0.01,0.01,0.01) 
		else
			print("the fireball has been fired!")
			break;
		end
	end
end)

5 comments

1

Why are you using UserInputService on the server?

0

you cant use

0

user imput service

0

on the server side

0

Because I want others players to see the fireball changing its size.

2 answers

✓ Accepted answer

UserInputService is client-sided; if you need to know if a client has a specific key down, you could use a remote function.

You cannot transmit Services though RemoteEvents. Instead just make an event inside of the player with Instance.new and just make the fireball creation part the remotevent.

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