(SOLVED)Random Map System Error

Hello Developers!

The issue is the code randomizes the map for every player. Causing everyone to have a different gamemode. It is supposed to randomize the map for one player and replicate it to the other players.

I have looked for solutions online and on Devforum, and even executed the code differently. It is executed from a button on an admin panel(because I don’t really know when the gamemode will end)

Here is the code:

local TS = game:GetService("TweenService")
local broadcast = script.Parent.Frame
local num = 0
function createNum()
	local numChosen = math.random(1, 6)
	print(num)
	num = numChosen
end
function broadcasts(minigame)
	TS:Create(broadcast.ImageLabel, TweenInfo.new(0.2), {ImageTransparency = 0}):Play()
	wait(0.3)
	broadcast.ImageLabel.background.Text = minigame
	broadcast.ImageLabel.background.main.Text = minigame
	TS:Create(broadcast.ImageLabel.background, TweenInfo.new(0.2), {TextTransparency = 0}):Play()
	TS:Create(broadcast.ImageLabel.background.main, TweenInfo.new(0.2), {TextTransparency = 0}):Play()
	wait(1)
	TS:Create(broadcast.ImageLabel, TweenInfo.new(0.2), {ImageTransparency = 1}):Play()
	TS:Create(broadcast.ImageLabel.background, TweenInfo.new(0.2), {TextTransparency = 1}):Play()
	TS:Create(broadcast.ImageLabel.background.main, TweenInfo.new(0.2), {TextTransparency = 1}):Play()
end

game.ReplicatedStorage.NewGame.OnServerEvent:Connect(function()
	createNum()
	if num == 1 then
		local cloneSword = game.ReplicatedStorage.maps.Swordfight:Clone()
		cloneSword.Parent = game.Workspace.minigame
		broadcasts("Sword Fight")
	elseif num == 2 then
		local cloneObby = game.ReplicatedStorage.maps.obby:Clone()
		cloneObby.Parent = game.Workspace.minigame
		broadcasts("Obby")
	elseif num == 3 then
		local cloneMaze = game.ReplicatedStorage.maps.Maze:Clone()
		cloneMaze.Parent = game.Workspace.minigame
		broadcasts("Maze")
	elseif num == 4 then
		local clonepotato = game.ReplicatedStorage.maps.Tag:Clone()
		clonepotato.Parent = game.Workspace.minigame
		broadcasts("Tag, Your It")
	elseif num == 5 then
		local cloneFind = game.ReplicatedStorage.maps.Find:Clone()
		cloneFind.Parent = game.Workspace.minigame
		wait(0.2)
		local FindIt = game.ReplicatedStorage.RepItems["You Found It!"]:Clone()
		FindIt.Parent = game.Workspace
		broadcasts("Find it")
		local Extra = game.ReplicatedStorage.RepItems.ExtraPoint:Clone()
		Extra.Parent = game.Workspace
	elseif num == 6 then
		local clone = game.ReplicatedStorage.maps.Dodgeball:Clone()
		clone.Parent = game.Workspace.minigame
		local dodgeballs = game.ReplicatedStorage.RepItems.Dodgeball:Clone()
		dodgeballs.Parent = game.Workspace
		broadcasts("Dodgeball")
	end
end)

script that fires the remote event:

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.NewGame:FireServer()
end)

Thank you!

This line makes me wonder where this script is stored? Where exactly are you running this from?

The function createNum() looks like it’s entirely local, this value should be calculated on the server and sent to each client, i.e. supplied as a parameter to your local function, i.e.:

script.Parent.MouseButton1Click:Connect(function()
	local createdGameId = createNum();
	game.ReplicatedStorage.NewGame:FireServer(createdGameId);
end)

This should then be broadcast to all clients, with a single value. I only offer this advice since you have chosen to click a button for this to execute. Ideally you would have some timer system and a registration of all players to be involved in the game, this is simpler if all active players will always be involved. On the server, you simply countdown, get all available players, generate the game type (i.e. your createNum() function) and send a message to all clients with the result of the game type to make the correct map locally on all clients.

The script that contains the main code when the event is fired is in StarterGui(Mostly because im a new scripter and i was to lazy to do game.StarterGui but because ServerScriptService didnt work for some reason.) And I’ll try the solution thank you!
Edit: i have tried the solution and it doesn’t load the map. It loads the numbers and prints them, but doesnt actually load the map

I can’t even see what you are doing, I have no idea how you have split this code up. I could do this in a heartbeat but I cannot refactor your code until I see how it is that you are using it!

Alright. Thanks for helping me!

Button code:

function createNum()
	local numChosen = math.random(1, 6)
	print(numChosen)
end
script.Parent.MouseButton1Click:Connect(function()
	local num = createNum();
	game.ReplicatedStorage.NewGame:FireServer(num);
end)

Map loading code:

local TS = game:GetService("TweenService")
local broadcast = script.Parent.Frame

function createNum()
	local numChosen = math.random(1, 6)
end
function broadcasts(minigame)
	TS:Create(broadcast.ImageLabel, TweenInfo.new(0.2), {ImageTransparency = 0}):Play()
	wait(0.3)
	broadcast.ImageLabel.background.Text = minigame
	broadcast.ImageLabel.background.main.Text = minigame
	TS:Create(broadcast.ImageLabel.background, TweenInfo.new(0.2), {TextTransparency = 0}):Play()
	TS:Create(broadcast.ImageLabel.background.main, TweenInfo.new(0.2), {TextTransparency = 0}):Play()
	wait(1)
	TS:Create(broadcast.ImageLabel, TweenInfo.new(0.2), {ImageTransparency = 1}):Play()
	TS:Create(broadcast.ImageLabel.background, TweenInfo.new(0.2), {TextTransparency = 1}):Play()
	TS:Create(broadcast.ImageLabel.background.main, TweenInfo.new(0.2), {TextTransparency = 1}):Play()
end

game.ReplicatedStorage.NewGame.OnServerEvent:Connect(function(num)

	if num == 1 then
		local cloneSword = game.ReplicatedStorage.maps.Swordfight:Clone()
		cloneSword.Parent = game.Workspace.minigame
		broadcasts("Sword Fight")
	elseif num == 2 then
		local cloneObby = game.ReplicatedStorage.maps.obby:Clone()
		cloneObby.Parent = game.Workspace.minigame
		broadcasts("Obby")
	elseif num == 3 then
		local cloneMaze = game.ReplicatedStorage.maps.Maze:Clone()
		cloneMaze.Parent = game.Workspace.minigame
		broadcasts("Maze")
	elseif num == 4 then
		local clonepotato = game.ReplicatedStorage.maps.Tag:Clone()
		clonepotato.Parent = game.Workspace.minigame
		broadcasts("Tag, Your It")
	elseif num == 5 then
		local cloneFind = game.ReplicatedStorage.maps.Find:Clone()
		cloneFind.Parent = game.Workspace.minigame
		wait(0.2)
		local FindIt = game.ReplicatedStorage.RepItems["You Found It!"]:Clone()
		FindIt.Parent = game.Workspace
		broadcasts("Find it")
		local Extra = game.ReplicatedStorage.RepItems.ExtraPoint:Clone()
		Extra.Parent = game.Workspace
	elseif num == 6 then
		local clone = game.ReplicatedStorage.maps.Dodgeball:Clone()
		clone.Parent = game.Workspace.minigame
		local dodgeballs = game.ReplicatedStorage.RepItems.Dodgeball:Clone()
		dodgeballs.Parent = game.Workspace
		broadcasts("Dodgeball")
	end
end)

And where is this code located in your workspace? (that is the most important issue!)

You cannot broadcast (you can but not easily!) from a single client by clicking a button on your client! If the code exists for you it exists for everyone else (if it is created locally!!!). The map creation code exists on your client, and every other players client. Think just a moment, where are these messages actually going? If you send them from your client and everyone else has the same code too, what happens then?

Its not really the broadcast function issue, Its just the map loading, It worked perfectly before, but now it doesnt work. It prints the numbers.
Edit: I have put a print and warn statement to see if the event is firing. The event is firing, but the number seems to be my username for some reason, not a number.
And the broadcast function works apperently

You are not returning anything here, so the only parameter received by the server will be the owning player of the client, change it to this!!

function createNum()
	local numChosen = math.random(1, 6)
	print(numChosen);
	return numChosen;
end

Alright thank you so much! This helps alot

If it solved your issue please mark a solution and close the post.

it hasn’t fully solved it yet, Its getting close, but not their just yet. Thank you though. Its just not loading any map just yet.

I have actually sorta solved it. Im going to close this post.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.