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

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

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

3 answers

Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
Edited 5 years ago

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

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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

0
i also hope this is in a server script or it wont change any values on the server Gameplayer365247v2 1055 — 5y
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 Mr_Unlucky 1085 — 5y
0
ye as i said, im not good with tables so idk exacly what they do, thought the table might be the check Gameplayer365247v2 1055 — 5y
Log in to vote
-1
Answered by 5 years ago

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!

0
he wants to fire it once its equipped, not just in the backpack Gameplayer365247v2 1055 — 5y
0
Gamer, alert is right. I want in the backpack. I already have one for equipped. AwesomeMrBird -79 — 5y
0
well you said otherwise in the question Gameplayer365247v2 1055 — 5y
0
"pick up a tool" not "equip a tool" AwesomeMrBird -79 — 5y

Answer this question