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)
This is the script I made so far. This script only works once I equip the tool. I want it to fire once the tool is in my inventory. Please help me write this script or give me the part I am missing and explain what it is.
In the code you added in the comments you defined the player using Players.LocalPlayer, and you also claimed its a server script (in the game we were playing), even though the LocalPlayer cannot be defined in server scripts.
local pantsTable = {"Yellow Pants","Blue Pants","Red Pants"} function plrHasPants(pantsName) for i,v in pairs(pantsTable) do if v == pantsName then return true end end return false end function checkItem() for i,v in pairs(game:GetService("Players"):GetPlayers()) do local pantsCollected = v.leaderstats:FindFirstChild("Pants Collected") for i,v in pairs(v.Backpack:GetChildren()) do local playerOwnsPants = plrHasPants(v.Name) if playerOwnsPants then pantsCollected.Value = pantsCollected.Value + 1 end end end end while wait(3) do checkItem() end
Every 3 seconds we get the player, loop through their backpack, and if the pants in their inventory exists in the pants table we add pantsCollected 1.
I also recommend you add a boolean to check if they had the pants before so it doesnt needlessly add to pants collected
local DonePlayers = {} script.Parent.Equipped:Connect(function() local Player = game.Players:GetPlayerFromCharacter(Tool.Parent) if Player and not DonePlayers[Player] then DonePlayers[Player] = true Player.leaderstats.stat.Value = Player.leaderstats.stat.Value + 1 end end)
here is a simplified version of what you got going on there, get the player from the tool parent just as u did but it gets the player in just once line, you dont need to make a local Players from game:GetService("Players"). :GetService is actually just needed for services we dont see in explorer, after it gets the player it just goes directly to the leaderstats and finds the stat and adds 1 to it, hope this works cause im not very good with tables yet so im a bit lost in that part
Read https://scriptinghelpers.org/questions/64599/i-need-to-check-if-a-tool-in-the-inventory-is-selected-without-using-toolequipped You can use this script
if player.Backpack:FindFirstChild("Tool name goes here") then print("Tool exists") --Put the code you want to run if they have the tool here end
If that doesn't work comment on this answer!