Attempt to index nil with 'IsA'

I have a spell in my game that sets the target on fire when you click them, but I have an error

When I click, it errors with Attempt to index nil with 'IsA'. This error shows if i click on terrain or an object

Heres the script

local player = game.Players.LocalPlayer
local debounce = false
local character = player.Character
local root = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local fireevent = game.ReplicatedStorage.Spells
local spell = false
local loadedSpell = player:WaitForChild("LoadedSpell")

player.Chatted:Connect(function(msg)
	if string.lower(msg) == "ignis" and debounce == false and spell == false and player.Character.Ragdoll.Value == false and not player.Character.Humanoid.PlatformStand and player.Character.Humanoid.Health > 0  and player:FindFirstChild("Pyrokinesis") and loadedSpell.Value == "None" then
		spell = true
		loadedSpell.Value = "Ignis"
	end
end)

mouse.Button1Down:Connect(function()
	local mousetarg = mouse.Target
      if mouse.Target:IsA("BasePart") then -- errors here
	if mouse.Target.Parent:FindFirstChild("Humanoid") and debounce == false and spell == true and player.Character.Ragdoll.Value == false and not player.Character.Humanoid.PlatformStand and player.Character.Humanoid.Health > 0  and player:FindFirstChild("Pyrokinesis") and loadedSpell.Value == "Ignis" then
		debounce = true
		if (mousetarg.Position - root.Position).magnitude <25 and debounce == true then
			fireevent:FireServer("Ignis", mousetarg)
			loadedSpell.Value = "None"
			wait(15)
			spell = false
			debounce = false	
			end
		end
	end
end)

I believe this is because mouse.Target is equal to nil.

You need to make sure that mouse.Target is not nil if you want it to not get an error when pointing at things like the sky or terrain.

That’s because the Target is nil. You can try using Raycasting. If you don’t know anything about that, there are topics you can look at from the past and also the dev hub that discuss raycasting.

(Personally know nothing about raycasting, so I can’t help you regarding it, sorry. :grimacing:)

So how would i fix it and stop it from erroring? I’m clicking on something so it shouldnt be nil, and if i click on another player it works fine, it just errors if i click on something that isnt a player

try if mouse.Target ~= nil and mouse.Target:IsA("BasePart") then

1 Like

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