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!
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!
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