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