"Attempted to index nil with Attachment" even though Attachment exists

Great! More coding, more stupid errors. I’m currently coding a magnet and well

  1. its vulnerable against exploiters
  2. the client side visual effects aren’t working.
    image
    (even though attachment exists and I EVEN USED WAITFORCHILD!)
    Client:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local magnetEvent = script.Parent.MagnetEvent

local tool = plr.Backpack:FindFirstChildOfClass("Tool")
if tool:GetAttribute("magnet") then
	tool.Equipped:Connect(function(mouse)
		magnetEvent:FireServer(tool)
	end)
end

magnetEvent.OnClientEvent:Connect(function(plr,coin)
	local alignPos = Instance.new("AlignPosition",script.Parent.Handle.coinPart.Attachment)
	local beam = workspace.FX.MagnetBeam:Clone()
	local tickA = tick()
	beam.Parent = tool
	beam.Attachment0 = coin.Attachment
	beam.Attachment1 = script.Parent.Handle.coinPart.Attachment
	alignPos.MaxForce = 2000
	alignPos.RigidityEnabled = false
	alignPos.MaxVelocity = tool:GetAttribute("range") * 8
	coin.Anchored = false
	coin.Massless = true
end)

Server:

script.Parent.MagnetEvent.OnServerEvent:Connect(function(plr,tool)
	local char = plr.Character
	while task.wait(0.1) do
		local o = OverlapParams.new()
		o.CollisionGroup = "Coins"
		local result = workspace:GetPartBoundsInBox(char.HumanoidRootPart.CFrame,Vector3.one * (tool:GetAttribute("range") * 2),o)
		if result ~= {} then
			for i,v in pairs(result) do
				local coin = result[i]
				plr:WaitForChild("leaderstats").Coins.Value = coin:GetAttribute("value")
				script.Parent.MagnetEvent:FireClient(plr,coin)
			end
		end
	end
end)

I’m pretty sure I’m overthinking this as I almost never use FireClient() but I don’t know what else to do.
Edit: you may want the hierarchy of the tool
image

1 Like

When firing the client, you don’t need to include the player parameter on the client side

Well actually it does require it for :FireClient() but I don’t need to pass on the parameter

The error tells you that the coin doesn’t exist, therefore giving error attempt to index nil with [item] (or coin is nil)

OnClientEvents don’t take the player as it’s first argument unlike the server, remove that and it will work

what i mean is that when using FireClient() you include player in the first argument

but when invoking in the client, you dont need to inluce the player in OnClientEvent since the parameters of it is the second and more arguments you passed on FireClient()

Yep, when I was replying to @T3_MasterGamer I realized that.
Ok tiny rant time: for the past few posts, multiple people have answered it and you cannot select multiple answers!

Again: Thank you both!

1 Like

Oh yea, one thing, because im using a while loop, the beam get cloned a lot, should I be worried about that?

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