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

Why isn't my script accessing my player's backpack?

I'm trying to access a certain player's backpack from Workspace and it's not working. I've tried a print statement as you can see below, but nothing is showing in the output. I give thanks and credit to saSlol2436 for helping me make this script.

local hasFlag = true

function accessBackpack(hit)
    if hit and hit.Parent then
        local chara = hit.Parent
        local player = game.Players:GetPlayerFromCharacter(chara)
        local backpack = player:FindFirstChild("Backpack")
        local flag = backpack:FindFirstChild("RedFlag")
        if flag and flag.Parent == backpack and hasFlag == true then
        else
			print("RedFlag is not apparant")
        end
    end
end 

script.Parent.RedFlag.Handle.Touched:Connect(function(char) 
    if char ~= nil then 
        local player = game.Players:GetPlayerFromCharacter(char.Parent) 
        if player then--check if they are a player
            if char.Parent:FindFirstChild("Humanoid") then 
                if char.Parent.Humanoid.Health ~= 0 then--if they are still alive
                    print(player.Name .. " has the Red Flag!")
          char.Parent:FindFirstChild("Humanoid").Died:Connect(function()
    if char.Parent:FindFirstChild("RedFlag") or accessBackpack() then --if they die and they have the flag in their hand or backpack
        local FlagClone = game:GetService("ServerStorage"):WaitForChild("RedFlag"):Clone()--the flag gets cloned and put into workspace
        FlagClone.Parent = game.Workspace
		FlagClone.Handle.CFrame = CFrame.new(char.Parent.Head.Position)
		FlagClone.Handle.Anchored = true
    end                                      
end)
                end
            end
        end
    end
wait(2)
end)

2 comments

0

You should print debug all the values, see what they are worth, and see if anything is wrong after that.

0

So like: print(backpack.Parent) and see who the parent is? Because I've already tried that and nothing in the output

2 answers

So there are several problems in this code. You didn't exactly state what functionality you were looking for so it will be up to you to fix it.

You use accessBackpack on line 24 as if it returns something (see this for what that means) but it doesn't. Your if statement won't execute the code within it because it will never be true.

if char.Parent:FindFirstChild("RedFlag") or accessBackpack() then --if they die and they have the flag in their hand or backpack

The second problem is that when you call accessBackpack() (see above) you are not passing it an argument of hit. I assume that this is some part of the character (your code is quite unclear what hit is supposed to be). If you don't pass arguments to functions, they will assume that you meant to pass nil.

If we look at the following line from accessBackpack we can see why that's a problem.

function accessBackpack(hit)
    if hit and hit.Parent then

The if statement will not run your code because hit is nil (it acts like false).

4 comments

0

So what do I do?

0

This comes down to you not scriptinghelpers. I have no idea what you want your code to do so YOU have to figure out how to get your code working the way you want it to. I suggest messing around with values until you understand how your code works.

0

I want to run a check to see if the player has the flag in hand or in backpack

0

I'm not very advanced in scripting. I just started a couple weeks ago.

(Comment) sry i cant post comments cuz of my - stuff i noticed when u equip stuff its in workspace in your Character

4 comments

0

yes, but when the player is not holding it in their hand, its in their backpack, so I need to have a check that checks if its in their hand or backpack

0

maybe u can do 2 different scripts or 2 scripts in one that checks if its in backpack or in Workspace

0

Wow, that is inefficient.

0

Do u know what to do hiimgoodpack?

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