When printing my Attachment1's value, it calls out nil for some reason

Whatsup developers,

personally it’s been a while since I’ve scripted and I stumbled against a problem here:
At my task.defer function (function is at the bottom) it keeps printing nil, while the Attachment1’s value is not nil. Could anyone explain why it still prints nil? Did I do something wrong with the playerAttach variable? Attachment1 should be the player by the way.

Please ask for more background information if you need that to help me.

Thanks for helping in advance.

local touchpart = script.Parent
local beam = touchpart.Beam
local highLight = touchpart.Highlight
touchpart.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			beam.Attachment1 = hit.Parent.HumanoidRootPart.RootAttachment
			highLight.Enabled = true
	end
end)

task.defer(function()
	while task.wait(0.5) do
		if beam.Attachment1 ~= nil then
			local playerAttach = game.Players:GetPlayerFromCharacter(beam.Attachment1)
			print(playerAttach)
		end
	end
end)

The argument you pass through GetPlayerFromCharacter has to be the character model. Not a descendant of it.

What exactly do you mean? I can’t put “beam.Attachment1” in the argument? What do I put in the argument instead?

beam.Attachment1 in this case is hit.Parent.HumanoidRootpart.RootAttachment. This means the character Model instance of the player is beam.Attachment1.Parent.Parent since beam.Attachment1's parent is HumanoidRootPart and HumanoidRootPart's parent is the character model:

local playerAttach = game.Players:GetPlayerFromCharacter(beam.Attachment1.Parent.Parent)

Amazing! That worked, I see what I did wrong. Thanks a lot for explaining man.

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