Getting passed value is not a function error when I'm passing a function

Code:

	function touched(hit,pad)
		if pad then
			if canVote then
				if hit.Parent:FindFirstChild("Humanoid")then
					local char = hit.Parent
					local charplr = players:GetPlayerFromCharacter(char)

					if charplr:FindFirstChild("Vote") and charplr ~= nil then
						pad.Votes.Value += 1
						if charplr.Vote.Value ~= "" then
							local padToSub = voting:FindFirstChild(charplr:FindFirstChild("Vote").Value)
							padToSub.Votes.Value -= 1
						end
						charplr.Vote.Value = pad.Name
						module.updateDisplay()
					end

				end
			end
		end
	end
	connection = vote1.Touched:Connect(touched(vote1))
	connection2 = vote2.Touched:Connect(touched(vote2))
	connection3 = vote3.Touched:Connect(touched(vote3))

It’s erroring on the last 3 lines- and it says that the value passed is not a function. I don’t understand why this would be happening, as touched has just been stated as a function before.

Probably just a dumb error, but I haven’t been able to get it to work in the last hour.

Use this:

connection = vote1.Touched:Connect(function(hit)
	touched(hit, vote1)
end)
connection2 = vote2.Touched:Connect(function(hit)
	touched(hit, vote2)
end)
connection3 = vote3.Touched:Connect(function(hit)
	touched(hit, vote3)
end)
2 Likes

Huh. I had used nearly identical code in a different project I made, and it worked fine at the time. Thanks for your help anyways.

1 Like