Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why wont this click detector script work?

Hi I was making this script and I was wonder why this didn't work. Also it didn't have any output errors and it is in a local script. Can you help me with this script problem?

function OnClick()
local plr = game.Players.LocalPlayer
if plr:FindFirstChild("leaderstats") ~= nil then
				local clicks = plr:FindFirstChild("leaderstats"):FindFirstChild("Cow Clicks")
			if (clicks ~= nil) then
				clicks.Value = clicks.Value + 1
				script.Parent.Sound:Play()
			end
else
	print("leaderstats not found")
end

end

script.Parent.ClickDetector.MouseClick:connect(OnClick)

1 answer

✓ Accepted answer

You don't need a LocalScript for this, neither should you use one. The ClickDetector's MouseClick event passes the player that clicks as an argument to its connected function

function OnClick(theguythatclicked)
	--Get that tabbing under control.
	--One function, one conditional statement,
	--one loop, etc. gets one tab.
	--You should let the IDE do it for
	--you for the most part.
	
	local clicks = theguythatclicked:FindFirstChild("leaderstats"):FindFirstChild("Cow Clicks")
	
	if clicks then
		clicks.Value = clicks.Value + 1
		script.Parent.Sound:Play()
	end
end

script.Parent.ClickDetector.MouseClick:connect(OnClick) --Event passing player to function

2 comments

0

didn't work ;( any ideas?

0

Did you get rid of the LocalScript and put it in a server script?

This discussion is preserved as it was. New answers, comments, and votes are closed.