Script is not resulting how I want it to work

Viewed 35

I've been having this problem for 2-3 weeks, and it has been one of the biggest, if not, the biggest problems I've faced during scripting.

What I want to do is make a normal script in a block that will get a player's backpack tools when they touch the block and put ALL of their tools in a folder in ReplicatedStorage.

However, I have never got the right results. It would end up putting ONLY ONE item in a folder, or simply do nothing. I have put print("Number") along the script to see if the script can successfully function parts of the script.

I've also altered the scripts and before I knew it, I've gotten from only a few lines to a lot of lines in the script.

If anyone is able to help me, I would be very happy. I am a Lua beginner so please don't judge me. :)

Here is my script:


script.Parent.Touched:Connect(function(hit)
    if debounce == true then
        debounce = false
        if hit.Parent:FindFirstChild("Humanoid") then
            local toolFolder = Instance.new("Folder")
            toolFolder.Name = game.Players:GetPlayerFromCharacter(hit.Parent).UserId
            print("1")
            wait(1)
            if game.ReplicatedStorage.PlayerTools:FindFirstChildWhichIsA("Folder") then
                print("1.25")
                if game.ReplicatedStorage.PlayerTools:FindFirstChildWhichIsA("Folder").Name == game.Players:GetPlayerFromCharacter(hit.Parent).UserId then
                    print("1.5")    
                    toolFolder:Destroy()
                else
                    local tools = game.Players.GetPlayerFromCharacter(hit.Parent).Backpack:GetChildren()
                    toolFolder.Parent = game.ReplicatedStorage.PlayerTools
                    toolFolder.Name = "toolFolder"
                    repeat
                        for i = 1, #tools do
                            tools[i].Parent = game.ReplicatedStorage.PlayerTools.toolFolder
                            toolFolder.Name = game.Players:GetPlayerFromCharacter(hit.Parent).UserId
                            print("2. Win!")
                        end     
                    until 
                    #game.Players.GetPlayerFromCharacter(hit.Parent).Backpack:GetChildren() < 1
                    wait(1)
                    debounce = true
                    end
            else
                local tools = game.Players.GetPlayerFromCharacter(hit.Parent).Backpack:GetChildren()
                toolFolder.Parent = game.ReplicatedStorage.PlayerTools
                toolFolder.Name = "toolFolder"
                repeat
                for i = 1, #tools do
                    tools[i].Parent = game.ReplicatedStorage.PlayerTools.toolFolder
                    toolFolder.Name = game.Players:GetPlayerFromCharacter(hit.Parent).UserId
                    print("3. Win!")
                end     
                until 
                    #game.Players.GetPlayerFromCharacter(hit.Parent).Backpack:GetChildren() < 1
                wait(1)
                debounce = true
                    end 
                end
            end
        end)

If you would want to test anything for whatever reason, make sure there is a Part with the script I pasted in the Workspace and a folder named "PlayerTools" in ReplicatedStorage. Thank you very much in advance. Sorry if this is really long.

1 Answers

i got it, you can thank me B) https://www.roblox.com/library/10921938627/stack-overflow-help. maybe i use a lot of things that you don't know but it work, actually. and i didn't understood what you wanted to do exactly but you can custom it now :) (too much edits sorry) but also you can use the keyword "pairs()" to do table loops, ex :

local t = {"e","tjz","hello","world"}
for i, v in pairs(t) do
    print(v)
end

console :

>e
>tjz
>hello
>world
Related