Im scriptings somethngs and there's an error

Hello im testing something but there’s an error idk how to fix, here’s the code:

local Char = game.Players.LocalPlayer.Character
local InputServ = game:GetService("UserInputService")
InputServ.InputBegan:Connect(function(input,event)
	if input.KeyCode == Enum.KeyCode.Space then
		local ray = workspace:Raycast(Char.Head.Position,Vector3.new(Char.Head.Position.X,Char.Head.Position.Y + 9000,Char.Head.Position.Z))
		local p = Instance.new("Part",game.Workspace)
		p.Position = ray.Position
	end
end)

and this is the error:
StarterPlayer.StarterCharacterScripts.Test:5: attempt to index nil with ‘Head’

  • the script is client

Since the script is inside StarterCharacterScripts, you can directly access the character with script.Parent

local Char = script.Parent
local Head = Char:WaitForChild("Head")

local InputServ = game:GetService("UserInputService")
InputServ.InputBegan:Connect(function(input,event)
	if input.KeyCode == Enum.KeyCode.Space then
		local ray = workspace:Raycast(Head.Position, Vector3.yAxis*9000)
		if ray then
			local p = Instance.new("Part")
			p.Position = ray.Position
			p.Parent = workspace
		end
	end
end)

and btw, it seems strange to me that it doesn’t say:

workspace[<player name>].Test:5: attempt to index nil with 'Head'

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.