PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Buttons and Points

Buttons and Points

Jun 29 2018, 4:51 PM PST

Problem

You want to create a button that when you touch it, you earn points.

Solution

Create a leaderboard, and edit the point values when Touched event fires.

local enabled = true
 
game.Players.PlayerAdded:connect(function(player)
	local leader, score = Instance.new('IntValue'), Instance.new('IntValue')
	leader.Name = 'leaderstats'
	score.Name = 'Score'
	score.Parent = leader
	leader.Parent = player
end)
 
Workspace.Button.Touched:connect(function(hit)
	local p = game.Players:FindFirstChild(hit.Parent.Name)
	if pl and enabled == true then
		enabled = false
		p.leaderstats.Score.Value = p.leaderstats.Score.Value + 5
		wait(20)
		enabled = true
	end
end)

Discussion

We first include a leaderboard to manage points that players get. Then we listen for the Touched event on some button in the Workspace. We check to see if it’s a valid Player that hit the button and we also use debounce to prevent the button from giving out too many points. Then we set leaderstats.Score.Value the value of that Score IntValue to be its own value plus 5.

Tags:
  • touched
  • event
  • script