currencyname = "stat" ----------------------------------------- if game.Workspace.tools.Tool.PickedUp = true then leaderstats.Value = stat.Value + 1 for i,V in pairs(game.Players:GetPlayers()) do if V:FindFirstChild("leaderstats") and V then V.leaderstats[currencyname].Value = V.leaderstats[currencyname].Vulue + 1 end end 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:
currencyname = "stat" ----------------------------------------- if 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. then leaderstats.Value = stat.Value + 1 -- stat and leaderstats haven't been defined here, so this will error 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(). if V:FindFirstChild("leaderstats") and V then -- this is good, except the 'and V' does nothing V.leaderstats[currencyname].Value = V.leaderstats[currencyname].Vulue + 1 -- and this line is great end end end
Also, you're probably better off using an event instead of an if
. So, let's give this another shot:
local currencyname = "stat" -- using 'local' is just good practice local Players = game:GetService('Players') -- This is the safe way of getting a service local Tool = script.Parent -- if you put this script inside the tool, you don't need to use game.Workspace and all that local DonePlayers = {} ----------------------------------------- Tool.Equipped:Connect(function() -- This will fire every time someone equips the tool 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 if Player and not DonePlayers[Player] then -- Make sure we actually found the player DonePlayers[Player] = true Player.leaderstats[currencyname].Value = Player.leaderstats[currencyname].Value + 1 -- then this line you wrote will work great! end end)
Comment if you have any questions, and if this helped you fix your problem, please click "accept answer."