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

How can I access a player's backpack from Workspace?

I've tried this script below, but its not working. The function called accessBackpack (line 3) is the one i need help with. I made an if statement (line 22) it's supposed to do a check to see if the Flag is in the player's hand or backpack.

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
        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)

Please help me.

2 answers

Problem 1: You call your function without passing anything. Your function expects "hit" as a parameter. Use accessBackpack(char) to fix this

Problem 2: Your function does not return. So you do your check, then what? In your case, nothing. Use a return statement to send back the result. Otherwise the result is nil all the time, something that is useless to you. the format for return is:

return value

You can pass as many values as you wish.

http://wiki.roblox.com/index.php?title=Function#Using_Return

8 comments

0

There was no error message in the output

0

Of course not, as that is not a lua error. It runs fine in Lua, just not the way you want it to.

0

I was reading 2 questions and must have confused the two on errors, sorry.

0

its ok

0

I have a question about the return statement. So by pass values, u mean like: return(RedFlag)? I don't really understand it. I've looked at the link and YouTube videos and I still don't understand it. And where would I put it? At the end of the accessBackpack() function? Please help me understand this.

0

you can put it anywhere and you can have more than 1, such as you could have 1 in an if statement and 1 in the else. Once you return, the function stops. Also no (), just return RedFlag

0

ok thx

0

I just did what u said and it didn't clone

local hasFlag = false

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
		print("Has Flag") -- Delete line if you have special plans
		else
		      print("No flag")
        end
    end
end 

script.Parent.RedFlag.Handle.Touched:Connect(function(char) 
	print("Touched")
	hasFlag = true
    if char and char.Parent 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()
	   print("Person who has flag died!")
    if (char.Parent:FindFirstChild("RedFlag") or accessBackpack())  and hasFlag == true then --if they die and they have the flag in their hand or backpack
	hasFlag = false
        local FlagClone = game,ServerStorage:FindFirstChild("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)

I really hope this works, if not, please ask questions in comments or chat (I have not really tested this out in studio)

5 comments

0

Everything is working fine except for the part where the flag is cloned. It didn't get cloned, and there is no errors in the output.

0

Please help me

0

You should NEVER return a print statement!

0

Well, fixed it!

0

Hopefully

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