Local function running before called

Hey ya! I’m having a problem with a local function, in a server script running before the function is even called. The local function itself is sort of working. (I’ll explain later)

local vetBuildings = workspace.VetBuildings
local vet1 = vetBuildings.Vet1
local vet2 = vetBuildings.Vet2
local vet3 = vetBuildings.Vet3
local vet4 = vetBuildings.Vet4
local vet5 = vetBuildings.Vet5
local vet6 = vetBuildings.Vet6

local plotClaimSensor2 = vet2.ClaimPlot2

local debounce = false

local function onPartTouched_Vet2(otherPart)

	if debounce == true then

	else
		debounce = true
		local otherPart = otherPart.Parent -- The part/object that made that collison	
		local humanoid = otherPart:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			
			local player = humanoid.Parent
			
			if player:FindFirstChild("BuildingTag") then
				
			else
				if vet2:GetAttribute("PlotOwner") == "" or vet2:GetAttribute("PlotOwner") == " " then	
					
					print("TESTS, 2nd")

					vet2:SetAttribute("PlotOwner", player)
					print("PASSED, 2nd ATTRIBUTE")
					print(player)
					plotClaimSensor2.Pole1.Transparency = 1
					plotClaimSensor2.Pole2.Transparency = 1
					plotClaimSensor2.Sensor.Transparency = 1
					plotClaimSensor2.Pole1.CanCollide = false
					plotClaimSensor2.Pole2.CanCollide = false
					plotClaimSensor2.Sensor.CanCollide = false
					plotClaimSensor2.Sensor.SurfaceGui.Enabled = false

					local buildingTag = Instance.new("StringValue")
					buildingTag.Name = "BuildingTag"
					buildingTag.Parent = player
					buildingTag.Value = "Vet2"

					print("Instance.newSTRING")
					debounce = false
					
				end
			 	
		    end
	
		end
		
	end
end


plotClaimSensor2.Sensor.Touched:Connect(function()
	print("Sensor been touched")
end)

The plotClaimSensor, is so far away. The prints, are being displayed into the output as soon as the game starts. I mean the model is being hidden using the transparent lines of code in the scripts above.

if this is a server script, u can’t do game.players.localplayer could be the reason why its nil

The server cannot access game.Players.LocalPlayer since it only works on the client and appears to be a nil value. Try using another solution

I fixed that, I completely forgot that you can’t use game.Players.LocalPlayer. Would there be a reason why the function is running before the function is even being called?

Perhaps the touched is somehow getting fired?

Is there anyway I could get around it? Or should I maybe make a look into making a roblox report about it? Besides that, there is nothing I can think of that would be causing that.

try doing a debug and just do

plotClaimSensor2.Sensor.Touched:Connect(function()
print("Sensor been touched")
end)

also your function doesn’t really have any specification on who should be touching it so a random part could be triggering the connection.

.Touched works fine actually, just make a check in the function it’s calling and check if the value that passes through the event is a player or in-game object.

I did that debug, and it was printed a number of times. I also forgot to say, I updated the in-game code which I forgot to edit the code above.

I updated the code above, would that check be good enough? The print, was put in there for debugging, and that print is printed a couple of times.