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

How to detect two tools in player's backpack?

Asked by 4 years ago

This is my script: I want to detect if two tools (MediterraneanAvenue and BalticAvenue) are there in the player's backpack when he touches a part and want to give my conditions accordingly. But when I tried this it didn't work! Can someone please help me?

local Part = script.Parent



    Part.Touched:Connect(function(HIT)
        local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)

    local FindMediterraneanAvenue = Player.Backpack:FindFirstChild("MediterraneanAvenue")
    local FindBalticAvenue = Player.BackPack:FindFirstChild("BalticAvenue")



        local H  = HIT.Parent:FindFirstChild("Humanoid")
        if H and not FindMediterraneanAvenue then
            Player.PlayerGui.MediterraneanAvenue.StartUp.Visible = true

    elseif H and FindMediterraneanAvenue and not FindBalticAvenue then
        print("You need to own all lots first.")

    elseif H and FindMediterraneanAvenue and FindBalticAvenue then
            Player.PlayerGui.MediterraneanAvenue.ClickBuild.Visible = true





        end
    end)


    Part.TouchEnded:Connect(function(part) -- leaves the part
    if game.Players:GetPlayerFromCharacter(part.Parent) then -- if the part leaving is a player
    local Player = game.Players:GetPlayerFromCharacter(part.Parent) -- the player
    if Player.PlayerGui.MediterraneanAvenue.StartUp.Visible == true then -- if the gui is open
        Player.PlayerGui.MediterraneanAvenue.StartUp.Visible = false
        if Player.PlayerGui.MediterraneanAvenue.ClickBuild.Visible == true then
            Player.PlayerGui.MediterraneanAvenue.ClickBuild.Visible = false  



end 
end
    end
    end)


2 answers

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
4 years ago

If you want to check for tools, you have to check both the backpack and character (since the tools are in the character if they are equipped). You also have to remember that 'Backpack' doesn't have a capital P. Here's an example function for checking for the tool:

local function playerHasTool(player, toolName)
    local tool = player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName)
    if tool then
        return true
    else
        return false
    end
end

thing.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if playerHasTool(player, "MediterraneanAvenue") and playerHasTool(player, "BalticAvenue") then
        --do stuff
    end
end)

Hope this helps!

0
should I put if playerHasTool(player,"MediterraneanAvenue") == true? QuantumxGeneral 25 — 4y
0
You don't need to, since true == true just evaluates to true anyway, so you can just do it like it is in the example. mattscy 3725 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

just do

if Player.Backpack:FindFirstChild("MediterraneanAvenue") and Player.Backpack:FindFirstChild("BalticAvenue") then ... end

Answer this question