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

How do I make this script fire once I pick up a tool?

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.

1 comment

3 answers

✓ Accepted answer

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

3 comments

0

i also hope this is in a server script or it wont change any values on the server

0

You should also add a boolean since the player can use this to unequip and equip the tool so he cant get more points

0

ye as i said, im not good with tables so idk exacly what they do, thought the table might be the check

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!

5 comments

0

he wants to fire it once its equipped, not just in the backpack

0

Gamer, alert is right. I want in the backpack. I already have one for equipped.

0

well you said otherwise in the question

0

"pick up a tool" not "equip a tool"

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