Hi, I'm currently having a problem where my 'if statement' doesn't detect the name of the map and instead just prints the error.
I'm relatively new to Lua (have been working on it for a couple of weeks now) and might just have overlooked something very simple here.
I would very much appreciate if anyone could fix this as it looks like it should work to me:
I have tried doing if map == "Map1" but this does not work either
(my function MovePlayers does work as I've tested it outside of the if statement so that's not the issue)
01 | local num = math.random( 1 , 2 ) |
02 | local map = game.ServerStorage:WaitForChild( "Map" ..num) |
03 | map:clone().Parent = game.Workspace |
04 |
05 | print (map) |
06 |
07 | if string.find(map, "Map1" ) then |
08 | MovePlayers(Vector 3. new( 1129.8 , 0.5 , - 6.1 ), Vector 3. new( 928.2 , 0.5 , 183.7 ), Vector 3. new( 784.1 , 0.5 , - 86.3 )) |
09 | elseif string.find(map, "Map2" ) then |
10 | MovePlayers(Vector 3. new( 1129.8 , 0.5 , - 6.1 ), Vector 3. new( 928.2 , 0.5 , 183.7 ), Vector 3. new( 784.1 , 0.5 , - 86.3 )) |
11 | else |
12 | error ( "No map found" ) |
13 | end |
Thanks for any help offered!
I'm relatively new to scripting as well, not as new as you are, but still new enough that I don't have a clue what . . .
1 | if string.find(map, "Map1" ) then |
does, but you might have forgotten to ask if the name of the part is the same as the string. Here's an example:
1 | if game.Workspace.Block.Name = = "StringNameHere" then -- Make sure to always include that you're asking about the name of the part, and not the part. |
2 | -- Anything you want it to do in here. |
3 | end |
Sorry that I don't know what that line necessarily means, but according to the error anyway, the problem is you're asking if the object itself is equal to a string, which results in an error.
Edit: Couldn't you just do . . .
1 | if map.Name = = "Map1" then |
2 | -- Teleport to map1 code here |
3 | elseif map.Name = = "Map2" then |
4 | -- Teleport to map2 code here |
5 | end |
Make a folder called maps instead, and make a models called spawns in the maps and you put parts in it.
01 | local Maps = game.ServerStorage.Maps:GetChildren() |
02 | local RandomMap = Maps [ math.random( 1 ,#Maps) ] |
03 |
04 | print (RandomMap.Name) |
05 |
06 | local Spawns = RandomMap:WaitForChild( "Spawns" ):GetChildren() |
07 |
08 | for _, v in pairs (game.Players:GetPlayers()) do |
09 | if v.Character then |
10 | local Spawn = Spawns [ math.random( 1 ,#Spawns) ] |
11 | local Character = v.Character:MoveTo(Spawn.Position) |
12 | end |
13 | end |