Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I add one more value to my leaderstats once I pick up a tool?

Asked by 4 years ago
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

0
Look at your script and try to find any error look. Before the line 12 you write Vulue correct and test it JpcPcGamer360 0 — 4y
0
I'm thinking that you can just make the extra value from the start and set it's value to nil or 0 then change it when the tool is picked up. OR you can try making a value with an invisible name then changing it's name :P develop_d 53 — 4y
0
Try removing 'and V.' If I am right I believe you are just calling a tables with all the players inside. B_rnz 171 — 4y
0
Oh yeah and change 'Vulue' to 'Value' B_rnz 171 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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."

0
I can spam press 1 and 0 to get more value. How do I fix that? AwesomeMrBird -79 — 4y
0
I want it to +1 my value when a tool goes in my inventory not when I hold it out. AwesomeMrBird -79 — 4y
0
@AwesomeMrBird I added a table "DonePlayers" that tracks who has picked up the tool. Now it'll only work the first time you equip it. ThatChristianGuy 27 — 4y
0
@ThatChristianGuy Can you make it so they don't have to equip, and it works when they pick up the tool. AwesomeMrBird -79 — 4y
Ad

Answer this question