"Attempt to connect failed: Passed value is not a function"

For context it’s just a button that’s supposed to change some things about the baseplate.
Everything was fine until I tried to make the baseplate damage players on it.

local Trigger = script.Parent
local Debounce = false
local MouseClicked = script.Parent.ClickDetector

function onMouseClick()
	
	local mathRandom = math.random(1,2)
	
	if not Debounce then
		
		Debounce = true
		
		math.random(1,2)
		
		if mathRandom == 1 then 
			local Humanoid = workspace.BetterBasePlates:findFirstChild("Humanoid")
			print("1")
			workspace.BetterBasePlates.Color = Color3.fromRGB(255, 0, 0)
			
			function onTouched(hit)
				
				wait(1)
				      if Humanoid~=nil then
					
						          Humanoid.Health = Humanoid.Health -1  
				            end
			  end
		end
			
		wait(8)
		
			workspace.BetterBasePlates.Color = Color3.fromRGB(0, 255, 0)
		end
		if mathRandom == 2 then
			
			print("2")
			workspace.BetterBasePlates.Color = Color3.fromRGB(255, 120, 0)
			
			wait(8)
			workspace.BetterBasePlates.Color = Color3.fromRGB(0, 255, 0)
		end
		
		wait(6)
		
		Debounce = false 
	end
	

MouseClicked.MouseClick:Connect(onMouseClick)
workspace.BetterBasePlates.Touched:connect(onTouched)

The function OnTouched shouldn’t be created inside of the other function, create both your functions separately.

Why is this line just floating in there?

Your function is being created inside of the onMouseClick function locally - this means that from the scope of the script itself it cannot see it as its local to the onMouseClick function. As such you should either:

  • move it outside of the function.
  • move the second connection inside the function.

I would also suggest reformatting your code if this is the style you’ve adopted, its hard to read.

Oh I forgot it there when I was experiementing

local mathRandom = math.random(1,2)

is inside the first function so should I remove the local from the front of it or should I do it some other way?

Also I don’t understand how my style is different because I’m new.

Basically…

Things like math are global libraries so they can be referenced anywhere unless you redefine math locally. When your formatting code, aim for code that looks like this in style:

function setup_character(player, character)
	recalculate_values(player)
	local PlayerData = PlayerStats[player.Name]
	if PlayerData then
		local stats = PlayerData["stats"]
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.MaxHealth = stats.health
			humanoid.Health = humanoid.MaxHealth
		end
	end
end

Use your own style though, for example I like to capitalize important temporary tables. Indent using the tab key and just let the Roblox script editor do the formatting for you. You know your going fine if you have a nice line of end like mine. This makes it easier for your eyes, and it also makes it easier for you to understand issues.

Here is a reformatted version of your code:

local Trigger = script.Parent
local Debounce = false
local MouseClicked = script.Parent.ClickDetector

function onMouseClick()
	if not Debounce then
		Debounce = true
		local mathRandom = math.random(1,2)
		if mathRandom == 1 then 
			local Humanoid = workspace.BetterBasePlates:findFirstChild("Humanoid")
			print("1")
			workspace.BetterBasePlates.Color = Color3.fromRGB(255, 0, 0)
			local function onTouched(hit)
				wait(1)
				if Humanoid ~= nil then
					Humanoid.Health = Humanoid.Health -1  
				end
			end
			wait(8)
			workspace.BetterBasePlates.Touched:Connect(onTouched)
			workspace.BetterBasePlates.Color = Color3.fromRGB(0, 255, 0)
		elseif mathRandom == 2 then
			print("2")
			workspace.BetterBasePlates.Color = Color3.fromRGB(255, 120, 0)
			wait(8)
			workspace.BetterBasePlates.Color = Color3.fromRGB(0, 255, 0)
		end
		Debounce = false 
	end
end
	

MouseClicked.MouseClick:Connect(onMouseClick)

Tysm, I tried using the format you gave me and it looks so much nicer now.

1 Like