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

You get 1 click when you click the block, but it's adding clicks to everyone?

So when you click my block, you're suppose to get 1 Click (point) added to yourself on the leaderboard. The problem is that everyone gets a point anytime anyone clicks the block. How can I fix this?

My script:

function OnClick()

	local players = game.Players:GetPlayers()
	for i=1,#players do
		if (players[i]:FindFirstChild("leaderstats") ~= nil) then
			local clicks = players[i]:FindFirstChild("leaderstats")["Clicks"]
			if (clicks ~= nil) then
				clicks.Value = clicks.Value + 1
			end
		end
	end

	local f = Instance.new("Fire")
	f.Parent = script.Parent.Head
	game.Debris:AddItem(f, .2)

end

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

1 answer

✓ Accepted answer

###This is happening because you're looping through everyone in the game. The MouseClick event passes the player as a parameter, so use that instead.

local function OnClick(player) -- use more local functions

    player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1

    local f = Instance.new("Fire")
    f.Parent = script.Parent.Head
    game:GetService("Debris"):AddItem(f, .2)

end

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

###On a side note, RBXScriptSignal:connect() is deprecated, use RBXScriptSignal:Connect() instead.

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