01 | local brick = script.Parent |
02 |
03 | function onTouched(hit) |
04 | for i,player in ipairs (game.Players:GetPlayers()) do |
05 | if player.Character then |
06 | local stat = player:FindFirstChild( "leaderstats" ) |
07 | if stat then |
08 | player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 50 |
09 | brick:Destroy() |
10 | end |
11 | end |
12 | end |
13 | end |
14 |
15 |
16 | brick.Touched:connect(onTouched) |
I used this script and put it inside the part that gives coins, when the player touches it, it disappears and gives 50 coins, only problem is that it gives 50 coins to everyone.
I'm not really good with ipairs, so I can't really explain but instead try this:
01 | local brick = script.Parent |
02 |
03 | function onTouched(hit) |
04 | local hum = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if hum then |
06 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
07 | local stat = plr:FindFirstChild( "leaderstats" ) |
08 | if stat then |
09 | stat:FindFirstChild( "Coins" ).Value = stat:FindFirstChild( "Coins" ).Value + 50 |
10 | brick:Destroy() |
11 | end |
12 | end |
13 | end |
This should work, in your code you are getting all the players that are in the Player Service and not specifying which player, you could've also just added..
1 | if plr.Name = = hit.Parent.Name then |
2 | --do stuff |
3 | end |