✓ Accepted answer
The problem you were having was that you were getting all the players in the server and giving everyone the points. I can see why you did this, to get the player, but their is an easier way to get the Player who touched the brick. To do this, we use :GetPlayerFromCharacter() The character is the part that touches the brick, and so this would get the Player
local brick = script.Parent
function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local stat = player:WaitForChild("leaderstats")
if stat then
player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
end
end
end
brick.Touched:connect(onTouched)
As you can see in the script, I got the Player using :GetPlayerFromCharacter()and inside the () I put hit.Parent, which hit is the Character and Parent is the Characters parent, which is the Player
Another problem you may or may not face, is that the Wins value may not be created yet, so to fix this we just add a :WaitForChild()
local brick = script.Parent
function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local stat = player:WaitForChild("leaderstats")
if stat then
local wins = stat:WaitForChild("Wins")
if wins then
wins.Value = wins.Value + 1
end
end
end
end
brick.Touched:connect(onTouched)
This is just an extra pracaution, but it may not need to be used, but I would suggest using it, as some people might have slower connection, meaning that they may touch this brick before the Win value is created.
Hope this helped and be sure to +1 and accept answer.
1 comment
Edited to use code block.