What am I doing wrong here?

I’m trying to make a button to load a map. The button works perfectly fine but I just get the " [attempt to call a nil value]" error at line 11 in the server script.

local selected = maps:FindFirstChild(map)

I’ve looked on google and multiple forum posts and couldn’t find anything to fix this. Here’s the server script.

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local loadMap = replicatedStorage:FindFirstChild("LoadMap")

loadMap.OnServerEvent:Connect(function(player, map)
	print(player.Name..", ID of "..player.userId..", selected "..map)
	local mapsFolder = serverStorage:FindFirstChild("Maps")
	local maps = mapsFolder:GetChildren()
	
	for i = 1, #maps do
		local selected = maps:WaitForChild(map)
		local selectedParts = selected:GetChildren()
			
		local selectedMap = selected:Clone()
		selectedMap.Parent = game.Workspace
	end
end)

I’m not going to act like I know everything; I consider myself a noob at scripting. I’m absolutely clueless about how to fix this and it’s probably something that I just failed to see. Any help would be nice, and thank you in advance.

1 Like

Could I see your local script that’s firing to the server? Remember, that when you do :FireServer(), the first parameter will always be the player in the OnServerEvent function. So, for example, if you do :FireServer(map) that means the parameters in the OnServerEvent function will be (player, map) since it automatically passes the player as the first parameter.

If that isn’t the issue, then make sure that your explorer is set correctly and that whatever “map” is named actually exists in the mapsFolder.

1 Like

Here’s the local script

example.MouseButton1Click:Connect(function()
	local map = "Example"
	if active == true then
		LoadMap:FireServer(map)
	end
end)

I triple checked to make sure everything was in the right place

I don’t think you need to loop through the maps if the name of the map is being passed as an argument;

loadMap.OnServerEvent:Connect(function(player, map)
    local mapsFolder = serverStorage:FindFirstChild("Maps");
    local SelectedMap = mapsFolder:FindFirstChild(map);
        if SelectedMap then
            --<Code
        end;
end)

I don’t know if that is what you’re looking for.

1 Like

This works, I really appreciate it. I felt like I wasn’t going to be able to sleep until I could find an answer.

In addition to the looping, the main issue is that you call WaitForChild on an array of objects, which you cannot do.

local selected = maps:WaitForChild(map) --maps is an array of objects, so this won't work

Here is some modified code.

loadMap.OnServerEvent:Connect(function(player, map)
	print(player.Name..", ID of "..player.userId..", selected "..map)
	local mapsFolder = serverStorage:FindFirstChild("Maps")
	local maps = mapsFolder
	
	
	local selected = maps:WaitForChild(map)
	local selectedParts = selected:GetChildren()
			
	local selectedMap = selected:Clone()
	selectedMap.Parent = game.Workspace
	
end)

Ah, I’ll remember that next time. Thank you for letting me know.

1 Like