Help with variabless

So i’ll keep it straight, I have set a local variable for a script and when I receive a value for it I set it as that value. But when I check out that value it returns as nil.

local obj -- The variable
game.ReplicatedStorage.Events.TriggerEvent.OnClientEvent:Connect(function(objNew)
	obj = objNew
	print(obj) -- Prints Linkmon99
end)

script.Parent.MouseButton1Click:Connect(function()
	local hrp = game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
	if hrp and obj then -- obj seems to be nil here
		game.ReplicatedStorage.Events.HumanoidDescription:FireServer(obj)
	else
		print(hrp)
		print(obj)
	end
end)

while wait() do -- Just for testing to see if obj is nil or not
	print(obj) -- On first cycle prints nil, next cycle prints Linkmon99 and repeats
end

The obj value I am expecting here is the player character of Linkmon99 which is present in workspace. But with the while true loop I get the variable to be equal to nil the first time and the character the second.

Am I doing something wrong here or am I missing something?

while print nil/Linkmon99 every other time, did I understand you correctly?

Lets me put it like this,

while true do
print(obj) -- During first cycle prints nil, during next cycle prints Linkmon99 and repeats
end

Perhaps somewhere an event is called that interferes?

This is an almost new baseplate, with just that single remote event which changes the obj value.

What’s getting fired first, The TriggerEvent, or the MouseButton1Click?
If it’s the mouse click then obj won’t have been set yet.

game.ReplicatedStorage.Events.TriggerEvent.OnClientEvent:Connect(function(objNew)
	obj = objNew

The remote even is fired first that too just once, after which the mouse button is clicked.

Are you sure?

You show the output, but it only shows prints for line 18 and none of the other prints.
Try this with more descriptive print statements and it’ll show which event is firing first:

local obj -- The variable
game.ReplicatedStorage.Events.TriggerEvent.OnClientEvent:Connect(function(objNew)
	obj = objNew
	print("TriggerEvent: ", obj)
end)

script.Parent.MouseButton1Click:Connect(function()
	local hrp = game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
	if hrp and obj then -- obj seems to be nil here
		game.ReplicatedStorage.Events.HumanoidDescription:FireServer(obj)
	else
		print("Click detected. hrp = " , hrp, " obj = ", obj , " objNew = ", objNew)
	end
end)