I have a problem with RemoteEvents

Hi, basically, in my game, there’s a main server script that handles the game, and it will clone the number of maps needed from ServerStorage ( 2 clones for 2 players ), the problem is since the script are all the same, they will both listen to events and in the case of the map, will use the first string of numbers that a player has typed in, and mess up everything.

I’d like to know if there’s a way to avoid that without generating a RemoteEvent for each map

script :

local remote = game.ReplicatedStorage.RemoteEvents.EnableKeycode
local TweenService = game:GetService("TweenService")
local door = script.Parent.Parent
local doorCharniere = door.PrimaryPart
local doorTweenInfo = TweenInfo.new()

remote.OnServerEvent:Connect(function(player,text)
	local CodeValue = door.Parent.Parent.PC.Screen.Code
	local GUI = player.PlayerGui.KeycodeGUI
	local fix = GUI.Frame.Frame.CodeSee
	local doorCharTween = TweenService:Create(doorCharniere, doorTweenInfo, {CFrame = doorCharniere.CFrame * CFrame.Angles(0, math.rad(120), 0)})
	if tonumber(text) == CodeValue.Value then
			script.Parent.Correct:Play()
			fix.Text = "Correct code"
		    fix.TextColor3 = Color3.fromRGB(0, 200, 0)
		    wait(1)
			fix.Text = ""
			fix.TextColor3 = Color3.fromRGB(255, 255, 255)
			script.Parent.DoorOpen:Play()
			doorCharTween:Play()
			script.Parent.Prompt.Enabled = false
		else
			script.Parent.Incorrect:Play()
			fix.Text = "Incorrect Code"
			fix.TextColor3 = Color3.fromRGB(200, 0, 0)
			wait(1)
			fix.Text = ""
			fix.TextColor3 = Color3.fromRGB(255, 255, 255)
	end 
end)

script.Parent.Prompt.Triggered:Connect(function(player)
	remote:FireClient(player)
end)

The script works fine when only 1 map is present

If this is not clear please tell me i’ll re-explain

You should add another object value parameter called doorInstance and another argument in the local script that fires the remote indicating which of the doors you are trying to unlock. Then, check if the doorInstance matchs with the door variable:

remote.OnServerEvent:Connect(function(player,text, doorInstance)
   if doorInstance == door then
      local CodeValue = door.Parent.Parent.PC.Screen.Code
      local GUI = player.PlayerGui.KeycodeGUI
      -- rest of the code...
1 Like

Thanks ! I don’t know why I didn’t think if this lol
I’m going to sleep i’mma script it tomorrow and if it works i’ll mark as solution

I think it’ll probably work but I didn’t really understand what should the object value parameter be used and same thing with the argument, could you explain again :sweat_smile: thanks

Sure, a parameter in remote events is data that the client share to the server when someone fires the event. When you fire the event and send the signal to the server, you have to include the indications of what door the client is trying to unlock (those indications are called arguments), the same way you did with “text” argument, just that you send it as an instance, the door you are trying to unlock. When the server recives the fire event that the client sent (called parameters), it can run the code on the server with the information the player provided.
You can view more about remote events here: Remote Functions and Events

Thanks it works great now ! I didn’t to the exact thing, I actually compared the player name, let me explain : On the workspace, I will set the player in the map on a stringvalue and then make an argument of the player’s name in the GUI ( script.Parent.Parent.Parent.Name ), and if they’re the same, they will make the script continue