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

Points on a leaderboard?

So I'm quite new to lua, but I've had some experience. I came across this

game.Players.PlayerAdded:connect(function(player)
	stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	
	points = Instance.new("IntValue", stats)
	points.Name = "Points"
	points.Value = 0
end)

script.Parent.Part.Touched:connect(function(hit)
	for i, v in pairs(game.Players:GetPlayers()) do
		
		if v == hit.Parent then
			points.Value = points.Value + 10
		end
	end
end)

It's supposed to add points to the leaderboard when each player touches a part. So in other words, it loops around all the players that are in the server, and if it's the right player, it gives him 10 points. But it doesn't seem to add points. Did I do something wrong? And if there's something that could be done to make it better, I'd be happy to know.

Regards, Techadox.

1 comment

1 answer

Ok..

  • You never defined points in the Touched event.

  • You should use the GetPlayerFromCharacter function to get the Player object.

  • Also, I suggest adding a Debounce, to prevent the code from bounce-firing.

local db = false --Debounce, default off.

script.Parent.Part.Touched:connect(function(hit)
	if not db then --if debounce is off, execute code
		db = true --turn on debounce
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then --Check if it's a player that touched
			local points = plr:FindFirstChild("Points") --find value
			if points then --check if it exists
				points.Value = points.Value + 10 --increment value
			end
		end
		db = false --turn off debounce
	end
end)

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