Log is still spawning after the first time

Hi,
I am trying to spawn a part called “Log” through a button only if there isn’t already a part called “Log” in the workspace. I typed the code below but “Log” keeps spawning even though Log already exists in the workspace. Not sure why this is happening and there are no errors. Can someone explain the error?

local X = game.Workspace.Part.ClickDetector

local found = game.Workspace:FindFirstChild("Log")
if found == nil then
	function onMouseClick()
	local Log = game.ReplicatedStorage.Log:Clone()
	Log.Position = Vector3.new(45.1, 0.5, 25.7)
	Log.Parent = game.Workspace
	Log.Transparency = .5
end

end



X.MouseClick:Connect(onMouseClick)

Thanks

You have to check every time the ClickDetector is clicked if there’s already a Log in the Workspace. You should move:

local found = game.Workspace:FindFirstChild("Log")
if found == nil then
end

To the inside of your function, “onMouseClick.”

1 Like

Have that if statement within the event

Thanks. Also if you could, what is the difference between = and ==. I know == is equal to but I can’t find = in the operators section of the wiki.

= is when it should be = and == is when it checks if something is = something

1 Like

Perfect that makes more sense.

Using “=” is a way of making a statement in a line of code.

local A = 1

This is a statement that tells the rest of the script “Whenever A is mentioned, we’re talking about the number 1, just so everyone is clear and understands that.”

Using “==” is a way of “asking” the question “is equal to.”

local A = 1
print(A == 1)

This prints “true” because as mentioned earlier, when A is referenced, the whole script understands that we’re talking about the number 1. The script then compares A (1) to the number 1 and says it’s true that they’re equal to one another.

1 Like