Map Voting Error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a game and need to make a map voting section, and am now making it pick a random 3 maps, and display it on my ui.

  2. What is the issue? Include screenshots / videos if possible!
    It’s printing what I scripted, but nothings changing with the UI.

Server Script:

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--// Folders \\--
local MapsFolder = ReplicatedStorage:WaitForChild("Maps")
local RoundEvents = ReplicatedStorage:WaitForChild("RoundEvents")

--// Remote Events \\--
local PickMapsEvent = RoundEvents:WaitForChild("PickMapsEvent")

-- Function to select random maps and send data to clients
PickMapsEvent.OnServerEvent:Connect(function(player)
	local maps = {}

	-- Select 3 random maps
	for i = 1, 3 do
		local map = MapsFolder:GetChildren()[math.random(1, #MapsFolder:GetChildren())]
		-- Create a map data table with Name and Image
		table.insert(maps, {
			Name = map.Name,
			Image = map:GetAttribute("Image")  -- Assuming you're storing image URLs in the "Image" attribute of maps
		})
	end

	-- Debugging: print the selected maps and their attributes
	print("Server: Selected maps:", maps)

	-- Fire the event to all clients with the selected maps
	PickMapsEvent:FireAllClients(maps)
end)

Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RoundEvents = ReplicatedStorage:WaitForChild("RoundEvents")
local PickMapsEvent = RoundEvents:WaitForChild("PickMapsEvent")

-- Listen for map data
PickMapsEvent.OnClientEvent:Connect(function(maps)
	-- Debugging: Ensure the maps data is received correctly
	print("Client: Received maps data", maps)

	-- Handle the UI update (assume your voting frame is ready)
	local VotingFrame = game.Players.LocalPlayer.PlayerGui.RoundInfo.Voting
	if not VotingFrame then
		print("Error: VotingFrame is not found!")
		return
	end

	-- Check if we have map frames to update
	local mapFrames = VotingFrame:GetChildren()
	if #mapFrames == 0 then
		print("Error: No map frames found inside VotingFrame!")
		return
	end

	-- Debugging: print the names of map frames in the Voting frame
	for _, frame in ipairs(mapFrames) do
		print("Map Frame found:", frame.Name)
	end

	-- Assign maps to map options
	for i, mapData in ipairs(maps) do
		local mapFrame = mapFrames[i]
		if mapFrame and mapFrame.Name ~= "UICorner" and mapFrame.Name ~= "Title" then
			print("Updating frame:", mapFrame.Name)  -- Debugging
			local previewImage = mapFrame:FindFirstChild("Preview")
			if previewImage then
				previewImage.Image = mapData.Image
				print("Set image to:", mapData.Image)
			else
				print("Error: Preview image not found in frame:", mapFrame.Name)
			end

			local mapName = mapFrame:FindFirstChild("MapName")
			if mapName then
				mapName.Text = mapData.Name
				print("Set map name to:", mapData.Name)
			else
				print("Error: MapName label not found in frame:", mapFrame.Name)
			end
		end
	end
end)

-- Check if the event listener is correctly set up
print("Client: Event Listener is active")

My explorer:

Im not sure if this is the issue, but your code only checks the map frames up to the amount of maps you have, but does not account for the fact that the UICorner and Title objects exist.

You could try looping through the mapFrames instead, aswlell as adding a separate index outside the loop that only increases when the code gets past the check for UICorner and Title.

1 Like

Do you know how can I do this?

This is what I was thinking:

local i = 1 <-- create a variable to store the current index

-- loop through the children of the voting frame, we don't need the loops index so use an '_' 
for _, mapFrame in ipairs(mapFrames) do 

	-- if the child is the uicorner or title the index wont increase, because it is inside this if statement
	if mapFrame and mapFrame.Name ~= "UICorner" and mapFrame.Name ~= "Title" then
		local mapData = maps[i] <-- find the current map data
		i += 1 <-- increase the index