Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script giving points to only Player1 on the leaderboard?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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!

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 FirezDevv 162 — 4y
0
Yeah true RazzyPlayz 497 — 4y
0
Yes, this worked very well! Thank you! KingKobraTheBoss 4 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

The event MouseClick has a parameter that tell the script who clicked.

script.Parent.ClickDetector.MouseClick:connect(function(player)

--The player object is saved in the parameter player.
--do your things here

end)

https://developer.roblox.com/en-us/api-reference/event/ClickDetector/MouseClick

Answer this question