01 | currencyname = "stat" |
02 |
03 |
04 | ----------------------------------------- |
05 | if |
06 | game.Workspace.tools.Tool.PickedUp = true |
07 | then |
08 | leaderstats.Value = stat.Value + 1 |
09 |
10 | for i,V in pairs (game.Players:GetPlayers()) do |
11 | if V:FindFirstChild( "leaderstats" ) and V then |
12 | V.leaderstats [ currencyname ] .Value = V.leaderstats [ currencyname ] .Vulue + 1 |
13 | end |
14 | end |
15 | end |
This is the script I made so far. Please tell me how I can fix this. I have spent a lot of time on this script.
Output: https://gyazo.com/4388c7a1d164342262da174d9aed2fa5 Doesn't work: https://gyazo.com/542840e84dcb3d4ef0f1125c6c3c6871
Here's your script with comments on a few parts as to why it doesn't work:
01 | currencyname = "stat" |
02 |
03 |
04 | ----------------------------------------- |
05 | if |
06 | game.Workspace.tools.Tool.PickedUp = true -- For one, you need to use ==. One = sets a variable, two == checks for equality. Second, this kind of path (going through workspace and all) isn't that reliable. You should put a script inside the tool and use script.Parent. |
07 | then |
08 | leaderstats.Value = stat.Value + 1 -- stat and leaderstats haven't been defined here, so this will error |
09 |
10 | for i,V in pairs (game.Players:GetPlayers()) do -- lowercase v is kind of standard, but also you can use :GetPlayerFromCharacter() here instead of GetPlayers. When you use GetPlayers like this, everyone gets +1 stat instead of just you. Also, use :GetService(). |
11 | if V:FindFirstChild( "leaderstats" ) and V then -- this is good, except the 'and V' does nothing |
12 | V.leaderstats [ currencyname ] .Value = V.leaderstats [ currencyname ] .Vulue + 1 -- and this line is great |
13 | end |
14 | end |
15 | end |
Also, you're probably better off using an event instead of an if
. So, let's give this another shot:
01 | local currencyname = "stat" -- using 'local' is just good practice |
02 | local Players = game:GetService( 'Players' ) -- This is the safe way of getting a service |
03 | local Tool = script.Parent -- if you put this script inside the tool, you don't need to use game.Workspace and all that |
04 | local DonePlayers = { } |
05 | ----------------------------------------- |
06 | Tool.Equipped:Connect( function () -- This will fire every time someone equips the tool |
07 | local Player = Players:GetPlayerFromCharacter(Tool.Parent) -- Since the parent of the tool will be a player's character, we can use this function to get the relevant player |
08 | if Player and not DonePlayers [ Player ] then -- Make sure we actually found the player |
09 | DonePlayers [ Player ] = true |
10 | Player.leaderstats [ currencyname ] .Value = Player.leaderstats [ currencyname ] .Value + 1 -- then this line you wrote will work great! |
11 | end |
12 | end ) |
Comment if you have any questions, and if this helped you fix your problem, please click "accept answer."