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

My currency rewarding script gives previous player money instead of you, anyone know a fix?

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")["ParkBucks"]
			if (clicks ~= nil)  then
				script.Parent.ClickDetector.MaxActivationDistance = 0
				script.Parent.Transparency = 1
				clicks.Value = clicks.Value + 10
				script.Parent.Sound:Play()
				wait(20)	
				script.Parent.ClickDetector.MaxActivationDistance = 10
				script.Parent.Transparency = 0
			
			end
				
			end
		end
	end

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

I do not know what is wrong with my script, when it regenerates it gives the previous player that clicked it the money instead of the "clicker". Any help will be greatly appreciated.

1 answer

Instead of looping through game.Players, you can use the player parameter of MouseClick

script.Parent.MouseClick:Connect(function(playerWhoClicked)
    local leaderstats = playerWhoClicked:FindFirstChild("leaderstats")
    if leaderstats then
        local clicks = leaderstats.Clicks

        script.Parent.ClickDetector.MaxActivationDistance = 0
        script.Parent.Transparency = 1
        clicks.Value = clicks.Value + 10
        script.Parent.Sound:Play()

        wait(20) 
  
        script.Parent.ClickDetector.MaxActivationDistance = 10
        script.Parent.Transparency = 0
    end
end)

2 comments

0

This does not work

0

what doesnt work about it?

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