Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this teleport player script not working?

Asked by
cfiredog 274 Moderation Voter
8 years ago

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?

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? M39a9am3R 3210 — 8y
0
There is just one spawn at the moment. cfiredog 274 — 8y
0
So, you intend on adding more? M39a9am3R 3210 — 8y
0
Yes. I intend on making teams in the future, and each team will need a spawn location on each map. cfiredog 274 — 8y

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

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.
0
Thank you! This seems to have fixed things. I appreciate the help. cfiredog 274 — 8y
0
Like I said though, this is only a temporary fix until you start implementing teams. M39a9am3R 3210 — 8y
0
I will hopefully work things out when the time comes. cfiredog 274 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

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())]
0
The script posted above is an over-simplified version of the full script. The full script already has a random map chooser. cfiredog 274 — 8y

Answer this question