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

If statement not matching string with object name?

Asked by 5 years ago

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:

  • The error I get is as follows: bad argument #1 to 'find' (string expected, got Object)

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)


local num = math.random(1,2) local map = game.ServerStorage:WaitForChild("Map" ..num) map:clone().Parent = game.Workspace print(map) if string.find(map, "Map1") then MovePlayers(Vector3.new(1129.8, 0.5, -6.1), Vector3.new(928.2, 0.5, 183.7), Vector3.new(784.1, 0.5, -86.3)) elseif string.find(map, "Map2") then MovePlayers(Vector3.new(1129.8, 0.5, -6.1), Vector3.new(928.2, 0.5, 183.7), Vector3.new(784.1, 0.5, -86.3)) else error("No map found") end

Thanks for any help offered!

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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 . . .

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:

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.
-- Anything you want it to do in here.
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 . . .

if map.Name == "Map1" then
 -- Teleport to map1 code here
elseif map.Name == "Map2" then
-- Teleport to map2 code here
end
0
Hope I helped at least a little bit! Knineteen19 307 — 5y
0
Hi, yeah I used that string.find because I've been trying to fix this for a while now and really can't seem to get it, the problem is I can't just use the block's name as it's cloned in and might be different each time, this is similar to what you are suggesting but still doesn't work, really out of ideas!: if game.Workspace["Map"..num] == "Map1" then RIBBENTROPP 45 — 5y
0
I might be able to help you a little better if I knew what you're trying to do. Knineteen19 307 — 5y
0
Basically I have 2 models (maps) in the Server Storage and it selects one randomly by adding the random number to "Map", this is then cloned into the game and it is in the workspace as either Map1 or Map2, I'm basically trying to detect which one was selected and based on that it changes where the players are teleported into the maps, as they are different (right now the locations are the same) RIBBENTROPP 45 — 5y
View all comments (4 more)
0
FIXED IT! Damn your post did actually help a lot! I hadn't put .Name after if game.Workspace["Map"..num] which meant it wasn't the name of the part it was comparing, I can't thank you enough man! I'd upvote you if I could! RIBBENTROPP 45 — 5y
0
Oh, and I just put an edit showing what you could do xD. Knineteen19 307 — 5y
0
Yeah it's annoying when I don't know these little things in lua that make life so much easier haha, but I'm learning. Anyway thanks for your help again man and good luck with your own lua ability! RIBBENTROPP 45 — 5y
0
Thanks, you too! Knineteen19 307 — 5y
Ad
Log in to vote
0
Answered by
Shadrz 40
5 years ago
Edited 5 years ago

Make a folder called maps instead, and make a models called spawns in the maps and you put parts in it.

local Maps = game.ServerStorage.Maps:GetChildren()
local RandomMap = Maps[math.random(1,#Maps)]

print(RandomMap.Name)

local Spawns = RandomMap:WaitForChild("Spawns"):GetChildren()

for _, v in pairs(game.Players:GetPlayers()) do
      if v.Character then
             local Spawn = Spawns[math.random(1,#Spawns)]
             local Character = v.Character:MoveTo(Spawn.Position)
      end
end

Answer this question