How can i do this with tables?

Ive made a script whereas an event is fired and the argument of the event is sent to the client side, i want to make it so if the arguments name is the same name as a name in a table, something happens , but i dont know how to do it,
Screenshot 2024-11-20 171739
In this case if the finalmap argument name is the same name as beachmap1 in the watermaps table then the script will go on but if it doesnt then it wont go on

-- //Table
local WaterMaps = { "BeachMap1" }

Event.OnClientEvent:Connect(function(finalmap)

if finalmap == WaterMaps[1] then -- // 1 = "BeachMap1" 
print("something")
end

end)

i think thas correct idk, as long as finalmap is a string then it should work but idk - show what your args are

What I understand is you have a set (a set is a table where all values are unique) of water maps, and you’d like to check if your set contains a certain value.

Taking your example:

local waterMaps = { "BeachMap1" }
 -- you can obtain these in any way you'd like, for example through a remote like u have
local bm1, bm2 = "BeachMap1", "BeachMap2"

--[[
    use `table.find` to check if they're present in the set, `table.find` returns a numeric index if
    a value was found and `nil` otherwise
]]
local searchResult1 = table.find(waterMaps, bm1) 
local bm1Found = searchResult1 ~= nil --> true; it is in the set

-- you can achieve this in just 1 expression
local bm2Found = table.find(waterMaps, bm2) ~= nil --> false; it is not in the set

As for the script going on, you can just use a condition:

local mapFound = …

if mapFound then
    -- business logic that runs if the map was found
end

Side note, nil is falsy and number is truthy so you can theoretically omit the equality check:

local mapIndex = table.find(maps, mapName) --> a numeric index or `nil`

-- will treat presence of a numeric index as-if `true` and `nil` as-if `false`
if mapIndex 
    -- business logic that runs if the map was found
end

This is fine, though I think it’s a little less obvious and clear on intent, but you can use it if you’d like.

I dont think i explained in full detail, the remote event is fired from another script which randomly picks maps for the game using a randomiser, then sents the chosen maps name (finalmap) to the client side then depending on its name, the event does something, watermaps is just the name because those maps include water and for the water to work a script has to be enabled, like there are 4 maps in the game right now and only 1 includes water so i want the name of the finalmap to be sent to the client so it can enable the water scripts for the player, there could be more maps in the watermaps table added in future

How can i make it so the script works if theres more than 1 string (map) in the table?

You can add as many maps as you want and you would still be able to know whether any given map is a water map without changing anything downstream, just what maps are in the set.

-- //Table
local WaterMaps = {
[1]  = "BeachMap1",
[2] = "Map2",
[3] = "Map3",
[4] = "Map4",
 }

enableevent.OnClientEvent:Connect(function(finalmap)

if finalmap == WaterMaps[1] then -- // 1 = "BeachMap1"
print("something")
end

end)

i tried the if statement but when i did nothing seemed to print, i even tried printing the finalmaps name and it was exactly named beachmap1

show your server script that fires to client

if finalmap is a string this should work if its a table just use finalmap[1] (what index matches the other table index) then yh

this should work

-- //Table
local WaterMaps = {
[1]  = "BeachMap1",
[2] = "Map2",
[3] = "Map3",
[4] = "Map4",
 }

enableevent.OnClientEvent:Connect(function(finalmap)

if finalmap == WaterMaps[1] then -- // 1 = "BeachMap1"
print("something")
end

end)

This is invalid, the subscript operator [i] will retrieve the index i not the value i.

what would the valid version be?

Yo!

Just add placeholders like:

local maps = {
    ["Campsite"] = true
}

Even better, use the placeholder for something like a description:

local maps = {
    ["Campsite"] = "Epic map!"
}

Or if thats not what you mean, make an Enum module script

local MyEnum = ...

what is a enum module script though?

He edited it to be valid, you can check the previous version count beside the pencil icon.

im not sure what ure asking but, maybe something like this?

local MapList = {
	"WaterMap",
}

local function ICheck(MapName)
	if table.find(MapList, MapName) ~= nil then
		return true
	else
		return false
	end
end

Basically your own Enum.

If you type Enum… you will get a list of things made by roblox

Now you can acheieve something similar by using a module script like:

-- This is the module script
local Maps = {}

Maps.Enum = {
  ["Campsite"] = "Campsite" -- Placeholder
}

return Maps
-- This is some other script
local Maps = require(...MapsModule)
local currentMap = Maps.Enum.Campsite

EDIT: By your own enum I mean this module script. I’m not sure if this term exists, i just made it

Your condition is entirely redundant.

if cond then
    return true
else
    return false
end

Is equivalent to:

return cond

I tried the script and it came up with this, i dont know why it happens even though the finalmap is the exact same name as the name in the table


Screenshot 2024-11-21 173759

but table.find returns the index of the given value

local MapList = {
	"BeachMap1",
}

local function ICheck(MapName)
	if table.find(MapList, MapName) ~= nil then
		return true
	else
		return false
	end
end

print(ICheck("BeachMap1"))

i tried it, it prints true, idk why urs doesnt, does the WaterMaps gets cleared at some point in time?