Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

4 comments

0

Look at your script and try to find any error look. Before the line 12 you write Vulue correct and test it

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

0

Try removing 'and V.' If I am right I believe you are just calling a tables with all the players inside.

0

Oh yeah and change 'Vulue' to 'Value'

1 answer

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

4 comments

0

I can spam press 1 and 0 to get more value. How do I fix that?

0

I want it to +1 my value when a tool goes in my inventory not when I hold it out.

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.

0

@ThatChristianGuy Can you make it so they don't have to equip, and it works when they pick up the tool.

This discussion is preserved as it was. New answers, comments, and votes are closed.