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

attempted to index nil with 'Instance' error when trying to raycast?

so i made like kind of mechanics in ninja legends 2 when you hit the ground after jumping it made like a crack but it just comes with the error attempted to index nil with 'Instance' Local Script :

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(k,p)
	if p then return end
	if k.KeyCode == Enum.KeyCode.Space then
		wait(0.2)
		local raycastparams = RaycastParams.new()
		raycastparams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
		raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
		local ray = workspace:Raycast(game.Players.LocalPlayer.Character.HumanoidRootPart.Position,game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.UpVector*-7,raycastparams)
		local part = Instance.new("Part")
    if ray.Instance ~= nil then
		part.Parent = workspace
		part.Anchored = true
			part.CFrame = CFrame.new(ray.Position,ray.Position + ray.Normal)
		end
	end
end)

1 answer

✓ Accepted answer

The error you're getting is saying that the ray hasn't interested anything yet you're trying to get the part that it's intersecting with. ray.Instance is the part that the ray intersected with. What you want to do is check if the ray has actually intersected anything by simply changing line 12 to:

if ray ~= nil then

You also might want to increase the ray length because I had to change the jump height to 2 studs for the ray to be able to hit anything.

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