Variable printing nil in one function but is printing a value in another function

I have a module script that opens windows. In the doorModel.Hitbox.Touched function, the print(locked) is printing nil, but in the door.new function, it prints a value. I don’t understand why it is printing in one place and not the other.

Code:

The door.new function is ran from another script.

local plrenter = script.Parent.PlayerEnter

local door = {}

function door.open(doorModel, number, plr, tool)
	doorModel:SetAttribute("Open", true)
	local amountofshards = math.random(4,10)

	doorModel.Window['glass shatter']:Play()
	for i,v in pairs(doorModel.Window.Window:GetChildren()) do
		if v:IsA('BasePart') or v:IsA('UnionOperation') then
			v:Destroy()
		end
	end
	for i = 1, amountofshards do
		local clone = game.ReplicatedStorage.Shard:Clone()
		clone.Parent = workspace.Fx
		local Yscale = math.random(1,5)
		local Zscale = math.random(1,5)

		clone.Size = Vector3.new(0.05, Yscale, Zscale)

		clone.Position = doorModel.ShardsSpawn.Position
		game.ReplicatedStorage.Remotes.TextEffect:FireClient(plr, clone)
		game.Debris:AddItem(clone, 60)
		i += 1

	end
	for i,v in pairs(doorModel.Window.CrackedWindow:GetChildren()) do
		if v:IsA('BasePart') or v:IsA('UnionOperation') then
			v.Transparency = 0
		end
	end
	local CollectionService = game:GetService("CollectionService")
	for i,v in pairs(workspace.Fx:GetChildren())do
		if v.Name == "Shard" then
			CollectionService:AddTag(v, "Currency")
		end
	end
	plr.PlayerGui.MainGui.TextLabel.Text = tostring(tonumber(plr.PlayerGui.MainGui.TextLabel.Text) + amountofshards)
	plrenter:Fire(number)
end
function door.new(roomModel, number, locked)
	local doorModel = workspace.Door:Clone()

	doorModel:PivotTo(roomModel.Exit.CFrame)
	doorModel:SetAttribute("Open", false)

	doorModel.WindowNumber.SurfaceGui.TextLabel.Text = string.format('%04d', number)
	if locked == true then
		print(number.. 'is locked')
		doorModel.WoodBoard["Meshes/Planks_Cube.001"].ProximityPrompt.Enabled = true
		doorModel.WoodBoard2["Meshes/Planks_Cube.001"].ProximityPrompt.Enabled = true
		for i,v in pairs(doorModel.WoodBoard:GetChildren()) do
			if v:IsA('BasePart', 'UnionOperation') then
				v.Transparency = 0
				v.CanCollide = true
			end
		end
		for i,v in pairs(doorModel.WoodBoard2:GetChildren()) do
			if v:IsA('BasePart', 'UnionOperation') then
				v.Transparency = 0
				v.CanCollide = true
			end
		end
	end


	doorModel.WoodBoard["Meshes/Planks_Cube.001"].ProximityPrompt.PromptButtonHoldBegan:Connect(function(plr)
		if plr.Character:FindFirstChild('Crowbar') then
			doorModel.WoodBoard["Meshes/Planks_Cube.001"]["Destroying A Car Crowbar 5 (SFX)"]:Play()
			local hum = plr.Character:FindFirstChild('Humanoid')
			local track = hum.Animator:LoadAnimation(workspace.Items.Crowbar.Handle.Use)
			track.Looped = true	
			track:Play()
		end
	end)

	doorModel.WoodBoard["Meshes/Planks_Cube.001"].ProximityPrompt.PromptButtonHoldEnded:Connect(function(plr)
		doorModel.WoodBoard["Meshes/Planks_Cube.001"]["Destroying A Car Crowbar 5 (SFX)"]:Stop()
		local hum = plr.Character:FindFirstChild('Humanoid')
		for _,anim in pairs(hum.Animator:GetPlayingAnimationTracks()) do
			if anim.Name == "Use" then
				anim:Stop()
				break
			end
		end
	end)

	doorModel.WoodBoard2["Meshes/Planks_Cube.001"].ProximityPrompt.PromptButtonHoldBegan:Connect(function(plr)
		if plr.Character:FindFirstChild('Crowbar') then
			doorModel.WoodBoard2["Meshes/Planks_Cube.001"]["Destroying A Car Crowbar 5 (SFX)"]:Play()
			local hum = plr.Character:FindFirstChild('Humanoid')
			local track = hum.Animator:LoadAnimation(workspace.Items.Crowbar.Handle.Use)
			track.Looped = true
			track:Play()
		end
	end)

	doorModel.WoodBoard2["Meshes/Planks_Cube.001"].ProximityPrompt.PromptButtonHoldEnded:Connect(function(plr)
		doorModel.WoodBoard2["Meshes/Planks_Cube.001"]["Destroying A Car Crowbar 5 (SFX)"]:Stop()
		local hum = plr.Character:FindFirstChild('Humanoid')
		for _,anim in pairs(hum.Animator:GetPlayingAnimationTracks()) do
			if anim.Name == "Use" then
				anim:Stop()
				break
			end
		end

	end)

	doorModel.WoodBoard["Meshes/Planks_Cube.001"].ProximityPrompt.Triggered:Connect(function(plr)
		if plr.Character:FindFirstChild('Crowbar') then
			for i,v in pairs(doorModel.WoodBoard:GetChildren()) do
				if v:IsA('BasePart' or 'UnionOperation') then
					v.Anchored = false
					v.CanCollide = false
				end
			end
			doorModel.WoodBoard["Meshes/Planks_Cube.001"]["Wood break 1"]:Play()
			wait(1)

			doorModel.WoodBoard:Destroy()

			if not doorModel:FindFirstChild('WoodBoard2') then
				if plr.Character:FindFirstChild('Crowbar') then
					plr.Character:FindFirstChild('Crowbar'):Destroy() 
				elseif plr.Backpack:FindFirstChild('Crowbar') then
					plr.Backpack:FindFirstChild('Crowbar'):Destroy()
				end
			end
		end	

	end)

	doorModel.WoodBoard2["Meshes/Planks_Cube.001"].ProximityPrompt.Triggered:Connect(function(plr)
		if plr.Character:FindFirstChild('Crowbar') then
			for i,v in pairs(doorModel.WoodBoard2:GetChildren()) do
				if v:IsA('BasePart' or 'UnionOperation') then
					v.Anchored = false
					v.CanCollide = false
				end
			end
			doorModel.WoodBoard2["Meshes/Planks_Cube.001"]["Wood break 1"]:Play()
			wait(1)
			doorModel.WoodBoard2:Destroy()
			if not doorModel:FindFirstChild('WoodBoard') then
				if plr.Character:FindFirstChild('Crowbar') then
					plr.Character:FindFirstChild('Crowbar'):Destroy() 
				elseif plr.Backpack:FindFirstChild('Crowbar') then
					plr.Backpack:FindFirstChild('Crowbar'):Destroy()
				end
			end
		end
	end)
	


	doorModel.Hitbox.Touched:Connect(function(hit)
		if doorModel:GetAttribute('Open') == false and hit.Parent:IsA('Tool') and hit.Parent.Name ~= 'Crowbar' then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
			game.ReplicatedStorage.Remotes.WindowBreakAttack:FireClient(plr)
			game.ReplicatedStorage.Remotes.WindowBreakAttack.OnServerEvent:Connect(function(player, var)
				
			if var == true then
					print(var)
					print(locked)
					if not locked then	
							door.open(doorModel, number, plr, hit.Parent.Name)
					end
					if locked then
						if not doorModel:FindFirstChild('WoodBoard') and not doorModel:FindFirstChild('WoodBoard2') then
								local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
								door.open(doorModel, number, plr,hit.Parent.Name)
						end
					end
				end
			end)
		end
	end) 
	


	doorModel.Parent = roomModel


	return doorModel
end



return door

I just found the issue, got to send the value through the remote event.

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