Why is printing self throwing an error?

Hello,
for a bit of context here: Im trying to make a trails handler that takes in a trail and throws it in the torso, that works!

What doesnt work or where did i go in the wrong? Well, printing out self throws an error: ServerScriptService.TrailHandler:50: attempt to concatenate string with nil
And yes im 100% sure im using the correct opperators.

My code in modulescript:

local trail = {}
trail.__index = trail
local name = "Trail_Attachment" 

function trail.new(char:Model)
	if char.Torso and char.Torso:FindFirstChild(name) then Destroy(char) end
	local self = setmetatable({}, trail)
	local torso:Part = char.Torso
	
	local a1 = Att(torso)
	local a2 = Att(torso)
	
	a1.WorldPosition = torso.Position + Vector3.new(0, 1.4, 0)
	a2.WorldPosition = torso.Position - Vector3.new(0, .2, 0)
	
	self.att1 = a1
	self.att2 = a2
end

function trail:Make(trail:Trail, char:Model)
	if trail == nil then
		Destroy(char)
	end
	local torso:Part = char and char.Torso
	local Clone = trail:Clone()
	
	Clone.Parent = torso
	--Clone.Attachment0 = self.att1
	--Clone.Attachment1 = self.att2
	print("self hier att1 is: " .. self.att1) -- here it throws error 
	print("self hier att2 is: " .. self.att2)
end

return trail

Remember that everything works, i just need to know why the print will throw an error

put return self at the end of the .new() function

sorry i misunderstood the post, the guy below probably has the actual solution

1 Like

You’re trying to concatenate (Combine string with string). But self.att1 is nil. Hence the error concatenate string with nil

So basically, self.att1 is nil.

If you wish to print them on the same line, without this error add a comma instead of the ..
Like this:

print("self hier att1 is: " , self.att1)

Im so stupid, but now i look into this, why am i not returning self?

Update: , works for printing, but its says nil when it shoudlen

how are you calling the :Make() function?

Like this:

local remote = game.ReplicatedStorage.Remotes.Trail
local handler = require(script.Parent.TrailHandler)
local trails = game.ReplicatedStorage.Trails

remote.OnServerEvent:Connect(function(plr, trail)
	local char = plr.Character
	handler.new(char)
	handler:Make(trail, char)
end)

Okay here, you need to return self


and update the ServerEvent function to look like this
image

i did, still returns nil i dont know why.

UpdateL: wwait i didnt read full instructions

1 Like

Still returns nil, do you know why?
Current server:

local remote = game.ReplicatedStorage.Remotes.Trail
local handler = require(script.Parent.TrailHandler)
local trails = game.ReplicatedStorage.Trails

remote.OnServerEvent:Connect(function(plr, trail)
	local char = plr.Character
	local newTrail = handler.new(char)
	handler:Make(trail, char)
end)

The client one sending remote:

local frame = script.Parent
local buttons = frame:GetChildren()

local remote = game.ReplicatedStorage.Remotes.Trail

for i, button in buttons do
	if button and button.Parent and button:IsA("GuiButton") then
		button.Activated:Connect(function()
			local trail = button and button.Trail
			
			remote:FireServer(trail.Value)
		end)
	end
end

Trail in the client one is a obj value referring to the trail

1 Like

handler:Make(trail, char)
should be

newTrail:Make(trail, char)
1 Like

Thank you bro, sorry for this dumb question, i uhh was trying to make something simple to learn more ab oop

2 Likes

No problem, OOP was confusing for me at first too

1 Like

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