Attempt to index nil with 'connect'?

Hey, hi, hello

Before I start, I know that there was post earlier today that had the same name, so sorry if that’s a bit annoying. I did look at that post and it’s not helping me with this issue.

So I have a sword where a localscript is creating this error

  21:34:05.673 - Players.Gashphul.Backpack.LinkedSword.Localscript:28: attempt to index nil with 'connect'

These are the bits making the error:

local tool = script.Parent

local replicatedstorage = game:GetService("ReplicatedStorage")

local modules = replicatedstorage:WaitForChild("Modules")

local killnotification = require(modules:WaitForChild("KillNotification"))
local events = tool:WaitForChild("Events")
local killnotification = events:WaitForChild("KillNotification")

tool.Equipped:connect(function()

killnotification.OnClientEvent:connect(function(targetname)
		killnotification:CreateNotification(targetname,"Slashed",Color3.new(85, 170, 255),tool.Parent)
	end)
end)

There’s another OnClientEvent function for a different remoteevent and that works fine. Can anyone help me?

:connect is deprecated; use

tool.Equipped:Connect(function()
    --your code here 
end)
1 Like

Hey, please specify which line is 28.

The error means that whatever you used :connect with is not existing (nil).

The tool may not actually be a Tool object (parent of script) or your killnotification variable isn’t actually a RemoteEvent.

Basically it’s indexing an event that isn’t existing. The events I am speaking of here are your Equipped event and OnClientEvent events.

2 Likes

Actually it shouldn’t matter. The proper way to format it is with capitalization, but it should not effect the functionality of the code.