Moving player to a specific part

Hey! I’ve recently got this code:

local parts = workspace.Folder:GetChildren()
local partsCollected = table.create(#parts, false)

for i = 1, #parts do
   if not partsCollected[i] then 
      humanoid:MoveTo(parts[i].Position) 
      -- if this line doesn't work, use:
      -- humanoid:MoveTo(workspace.Folder["Part"..tostring(i)].Position)
   end
end

What I basically want to do, is get all parts from a folder, check whether a part is already obtained and then move the player depending on the previous argument. (If part 1 is obtained, player gets moved to part 2)

The move function is working, however it completely ignores the check part. Even if there are 2 or more players, they both get moved to the same part. How could I fix that?

Also, is there a way of how I could prevent 2 players, that joined at the same time, moving on one part?

Where is humanoid being defined in this code?

Mb, pasted the old code. The right code is this one:

local parts = game.Workspace.ATPads:GetChildren()
local partsObtained = table.create(#parts, false)

game.ReplicatedStorage.ATEvents.MovePlayer.OnServerEvent:Connect(function(player)
	local humanoid = player.Character:WaitForChild("Humanoid")
	for i = 1, #parts do
		if not partsObtained[i] then
			humanoid:MoveTo(parts[i].Position)  
		end
	end
end)

humanoid.MoveToFinished:Wait() after invoking the move to should work.

1 Like

So it should look like this?

if not partsObtained[i] then
	humanoid:MoveTo(parts[i].Position)
    humanoid.MoveToFinished:Wait()
end

Yes, that should work.

MINIMUM CHARACTER LIMIT

So, I tried it in-game. Thanks for the idea, tho I don’t think that it really fixed anything.

Instead of the player automatically going to the next un-obtained part, the player goes thru all parts, after he stops.

It wouldn’t be such a big deal, if the check function worked. There’s still something wrong with the check function, as it moves the player on part1, regardless that there is a player standing on it already.

I don’t see a check function in the code you provided.

To be honest, another person made this code for me. Do you think you could find the exact problem in the code and maybe tell me how I could change it so that it actually adds anything to the table and checks it? As I’m not sure how to do it fully.

I need a detailed summery of what exactly you are attempting to achieve.

Alright. So basically, once a player joins, another script checks their rank in a group. If they have one specified rank, an event gets fired.

After the event gets fired, the script should loop through a folder with all the parts. It should then check what parts are obtained and if one is obtained, check the another one.

If the next part is not obtained (a player isn’t touching it), the player which just joined would get moved on the part. (If parts 1 and 2 are obtained, player gets moved to part 3)

However, I want it so it automatically moves the player to the first un-obtained part (so it doesn’t have to go over all parts). I’d also like to make a function, that prevents 2 or more players, that joined at the same time, from getting moved on the same part.

Sorry if that’s too much, just wanted to explain it really further.

local module = {}

function module.MovePlayerToNextUnobtainedPart(player, groupRank, groupId)
    player.GroupId = groupId -- Set the player's group ID

    if player:GetRankInGroup(groupId) == groupRank then
        local parts = workspace.Folder:GetChildren()
        local partsCollected = {}
        local partsLocks = {}

        for i, part in ipairs(parts) do
            local lock = Instance.new("BoolValue")
            lock.Name = "Lock"
            lock.Parent = part
            partsLocks[i] = lock

            if not part:IsDescendantOf(workspace.IgnoreFolder) and not partsCollected[i] then
                if not partsLocks[i].Value then
                    partsLocks[i].Value = true -- Lock the part
                    player.Character.Humanoid:MoveTo(part.Position)
                    partsCollected[i] = true -- Mark the part as obtained
                    partsLocks[i].Value = false -- Unlock the part
                    break -- Stop the loop after the first un-obtained part is found
                end
            end
        end
    end
end

return module

Implementation

local module = require(path.to.module)

local player = game.Players.LocalPlayer
local groupRank = "RankName" -- Replace with the name of the group rank
local groupId = 123456 -- Replace with the ID of the group

module.MovePlayerToNextUnobtainedPart(player, groupRank, groupId)

Thanks for the code! Though, what is workspace.IgnoreFolder?

Folder of what we want to ignore in workpsace.

What do you mean exactly? Or basically, do I have to create the folder… or? I’m a bit confused.

Here is some advice.

When learning to code, it can be helpful to break down complex problems into smaller, more manageable pieces. If you find yourself struggling with a problem, it may be a sign that you need to work on developing your skills and knowledge further. One approach is to focus on solving smaller problems that build up to the larger problem. By breaking down the problem into smaller pieces and working on solving each piece individually, you can gain the skills, knowledge, and confidence you need to tackle larger and more complex problems in the future. Remember that programming is a process of continuous learning and growth, and that every challenge you overcome is an opportunity to become a better programmer.

I gave you some code that will work for your solution, I will not hand hold you on how to implement that solution because that’s not how we learn.

1 Like

I don’t think you get me. I understand the code, I just don’t understand what you mean by “what we want to ignore in workspace”. I just want to use few parts for this function and ignore every other, so I don’t really know why your script has an ignore folder.

You can remove it if its not needed. This is just one way to do what you are trying to do.