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

Why is this teleport player script not working?

I have a very basic script that teleports players to a specific Vector3 position as seen here:

if player.Character then
player.Character:MoveTo(Vector3.new(5,5,5))
end

What I am trying to do, but cannot figure out, is how to have a more generalized Vector3 value so that the position will change based on which map is loaded and where the part "Spawn" is in that map. My attempt at the solution:

Maps = game.ReplicatedStorage.Maps:GetChildren() --Gets the map
Spawn = Maps.Spawn.Value.Value	 --Vector3Value parented to a part called "Spawn"

if player.Character then
player.Character:MoveTo(Vector3.new(Spawn)) --Attempt to use the Vector3 value
end

I get an error as a result of running the code above which says "attempt to index field 'Spawn' (a nil value). Is there something I am doing wrong?

4 comments

0

It's because you're trying to find what the script thinks is an index value Spawn in the Maps variable. Is there just one spawn, or are there multiple spawns?

0

There is just one spawn at the moment.

0

So, you intend on adding more?

0

Yes. I intend on making teams in the future, and each team will need a spawn location on each map.

2 answers

✓ Accepted answer

##Problem

So what is happening is GetChildren is a table of indexes and values. Hence there is that i,v in pairs often times used. Since GetChildren's indexes are all number values from 1 to however many objects are children under the object. The script is seeing something like:

{
    1 = Brick,
    2 = Brick,
    3 = Brick,
    4 = Spawn,
    --etc.
}

Since the index values are numbers and doing Maps.Spawn, the Spawn is the index the script is trying to find. However, since Spawn can never be an index for GetChildren, it will return a nil value.


##Solution

The best way I see to do this is just using FindFirstChild on the Maps model to find Spawn. This may be a temporary fix, however you would need to ask with further attempt of making this script separate spawns for teams.


##Final Script

Maps = game.ReplicatedStorage.Maps:GetChildren() --Gets the map
Spawn = Maps:FindFirstChild("Spawn")
SpawnVector = Spawn.Value.Value   --Vector3Value parented to a part called "Spawn"

if player.Character then
    player.Character:MoveTo(SpawnVector) --Since you claim Spawn (Now SpawnVector) was a Vector3Value object, having Vector3.new() is redundant.
end

######Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment below. If I made a mistake, or the script is still generating an error let me know so I may resolve it.

3 comments

0

Thank you! This seems to have fixed things. I appreciate the help.

0

Like I said though, this is only a temporary fix until you start implementing teams.

0

I will hopefully work things out when the time comes.

You called GetChildren() on Maps, GetChildren returns a table whereas I expect you want a random map from that folder? Here's a quick fix that would get a random map from the maps

Maps=game.ReplicatedStorage.Maps:GetChildren()[math.random(1,#game.ReplicatedStorage.Maps:GetChildren())]

1 comment

0

The script posted above is an over-simplified version of the full script. The full script already has a random map chooser.

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