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

Script giving points to only Player1 on the leaderboard?

I wrote a script that basically worked so that every time a player clicked on a specific part, it would give them one point. The part would change color, and there would be a 3 second cool down. However, when I tried testing it with 3 players, the points were only being assigned to Player1, regardless whether it was Player2 or Player3 that actually clicked the part. Here is the code I have below:

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

-- script for the part that the player clicks
script.Parent.BrickColor = BrickColor.new("Bright green")

enabled = false
script.Parent.ClickDetector.MouseClick:connect(function()
	if enabled == false then 
		enabled = true
		script.Parent.BrickColor = BrickColor.new("Really red")
		local player = game.Players:FindFirstChildOfClass("Player")
		local points = player.leaderstats.Points
		points.Value = points.Value + 1
		wait(3)
		script.Parent.BrickColor = BrickColor.new("Bright green")
		enabled = false
	end
end)

Help would be very much appreciated, thanks!

2 answers

✓ Accepted answer

Hello!

I have checked your code and have made a solution. You have defined the Player in the wrong way. You should use function onClicked(Player) instead! Also note: When using a connect function, use Connect instead as connect is deprecated!

Code for the leaderstatrs (ServerScriptService):

game.Players.PlayerAdded:connect(function(Player)
    local Statis = Instance.new("IntValue", Player)
    Statis.Name = "leaderstats"

    local Points = Instance.new("IntValue", Statis)
    Points.Name = "Points"
    Points.Value = 0
end)

Code for the part that's giving 1 point per click:

script.Parent.BrickColor = BrickColor.new("Bright green")

Enabled = false
function onClicked(Player)
	if Enabled == false then 
        Enabled = true
        script.Parent.BrickColor = BrickColor.new("Really red")
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1
		wait(3)
        script.Parent.BrickColor = BrickColor.new("Bright green")
       Enabled = false
	end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

Hopefully this fixs your issue!

3 comments

0

Your also picking Player 1 since your letting the script pick with Players:FindFirstChildOfClass, the first child it finds with that class it will pick and roblox likes to alphabetically assign them them number assign Xx_XSanderPlayXx has the perfect answer to it

0

Yeah true

0

Yes, this worked very well! Thank you!

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