Hello! I have encountered this issue while using collection service to move the player to a specific part in the workspace.
local function SpawnPlayerOnCheckpoint(Character: Model)
task.wait(.2)
local Player = Services.Players:GetPlayerFromCharacter(Character)
local leaderstats = Player:WaitForChild("leaderstats")
Character:MoveTo(Services.CollectionService:GetTagged("Checkpoint")[leaderstats.Stage.Value].Position)
end
local function Load(Player: Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = Player
leaderstats.Name = "leaderstats"
local Wins = Instance.new("IntValue")
Wins.Parent = leaderstats
Wins.Name = "Wins"
local Stage = Instance.new("IntValue")
Stage.Parent = leaderstats
Stage.Name = "Stage"
Stage.Value = 1
Player.CharacterAdded:Connect(SpawnPlayerOnCheckpoint)
end
The script should move me to the part named one, but for some odd reason, each time i rejoin it randomly picks a part from workspace and chooses that one.
as you can noticed that doesn’t look like checkpoint 1, if anyone has a solution i’d be happy to hear it.
My issue goes around the fact I can’t move the player to the value,
The method CollectionService:GetTagged(tagName) returns an array of all instances with the provided tagName. However, the ordering of this array is nondeterministic. As you can see from the screenshot of the table, the key (number in square brackets) does not correspond to the value in any meaningful way.
There’s two ways you could address this:
Sort the result of the array returned from GetTagged so that it’s keys are associated with the correct value.
Instead of relying on GetTagged organize the spawn parts into a Folder. Assuming the spawn parts are named such that the first checkpoint is named “1”, second “2”, third “3” and so on as it appears you have done already, you can locate the correct spawn with spawnLocations:FindFirstChild(tostring(stage)) where spawnLocations is a reference to the Folder containing all spawn parts and stage is the stage number that the player is currently on.