Remote Events are causing trouble!

Hello,

As the title says my remote events are causing a bit of trouble.

To give you some info:

I have a teleporter(not finished) that teleports players to another place. Now for the design of the portal I used billboardgui etc to make some textlables that says how many players are going to join the place and how much you need.

But after I finished scripting the text that says how many players are in the joining queue I tested it with 2 players and the server. And I came across a problem.

The portal is a 2 player queue so only 2 players can enter it.

Now the problem is that when 1 player enters the queue nothing bad happens the counter just says it has 1 player entry. Same for as the 2nd player enters it it says 2/2 players.

The problem shows up when a player or 2 leave the portal.

Here is a pic of that the textlable shows:

image

It says -36/2 players. Now if we do the math cause I had some print statements we can see this:

These are results from 1 player in the server not 2. But the problem is the same with 2 players the only difference is that it doubles the result

As you cansee we have 9 times “yes

And every time the player leaves it will do this: starting amount +1

So for example the first time as you can see we only got 1 time yes. The 2nd time we leave we got 1+1 and the 3rd time we got 2+1 etc.

Now if we add all of this up we would get: (1+1) + (2+1) + (3+1) + (4+1) + (5+1) + (6+1) + (7+1) + (8 +1)

This will add up to 44 now minus the entries we got 35 which is first of all 1 lower than what is shows on the players label.

This is probably irrelevant information but it may help. Idk.

Here are the scripts:

This is the script that handles the touch event:

local objChecker = script.Parent
local playerVal = script.Parent.player_Cnt


local playerCnt = script.Parent.player_Cnt

local hrp_Pos = Vector3.new(45.95, 5.77, 283.75)

local leavePortal_GMn_1v1 = Vector3.new(45.55, 4.7, 271.5)

local uis = game:GetService("UserInputService")

local repstorage = game:GetService("ReplicatedStorage")

local event = repstorage:WaitForChild("ControlsEvents").leavePortalEvent
local event2 = repstorage:WaitForChild("ControlsEvents").leavePortalEventLeave

objChecker.Touched:Connect(function(playerHit)
	print("well")
	local playerName = playerHit.Parent.Name
	local player = game.Players:WaitForChild(playerName)
	local char = playerHit.Parent
	local HRP = char:FindFirstChild("HumanoidRootPart")

	if playerHit.Parent.Name == player.Name then

		HRP.Anchored = true
		HRP.CFrame = CFrame.new(hrp_Pos)
		playerCnt.Value += 1 -- this adds a player to the value which will be set to text
		

		if HRP.Position == hrp_Pos then
			event:FireClient(player)
			
		end
	end
end)

event2.OnServerEvent:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local HRP = char:FindFirstChild("HumanoidRootPart")
	HRP.Anchored = false
	HRP.CFrame = CFrame.new(leavePortal_GMn_1v1)
	playerCnt.Value -= 1 -- samething here
end)

This is the localscript inside startercharacterscripts

local uis = game:GetService("UserInputService")

local player1 = game.Players.LocalPlayer

local playerGUI = player1:WaitForChild("PlayerGui")
local frameKeyL = playerGUI:WaitForChild("Lobby_Portals").Frame






local repstorage = game:GetService("ReplicatedStorage")

local event = repstorage:WaitForChild("ControlsEvents").leavePortalEvent
local event2 = repstorage:WaitForChild("ControlsEvents").leavePortalEventLeave



event.OnClientEvent:Connect(function(player)
	frameKeyL.Visible = true
	uis.InputBegan:Connect(function(input, gameProcessedEvent)
		if gameProcessedEvent then return end
		if input.UserInputType == Enum.UserInputType.Keyboard then

			if input.KeyCode == Enum.KeyCode.L then
				
				print("yes")
				event2:FireServer(player)
				frameKeyL.Visible = false
			end
		end
	end)
end)

And this is the script that chagnes the player label text:

local val = script.Parent.Parent.player_Cnt

local txt = script.Parent.Parent.Parent.portal_Mesh.BillboardGui.players_

while true do
	txt.Text = val.Value .. "/2 Players"
	wait(.1)
end

Also why I think it is remote events that are causing this is because when I leave a remote event is fired and the print statements are in the section where the “event.OnServerEvent” is located

That event will make sure the player leaves the portal. But it is called (1+1) +(2+1) etc etc.

So please help me with this and also PLEASE READ IT EVEN THOUGH ITS A LOT OF TEXT.

1 Like

I don’t see any debounce in your touched event. Try something like this:

debounce = {}
objChecker.Touched:Connect(function(playerHit)
	print("well")
	local playerName = playerHit.Parent.Name
    if debounce[playerName] then return end
    debounce[playerName] = true
	local player = game.Players:WaitForChild(playerName)
	local char = playerHit.Parent
	local HRP = char:FindFirstChild("HumanoidRootPart")

	if playerHit.Parent.Name == player.Name then

		HRP.Anchored = true
		HRP.CFrame = CFrame.new(hrp_Pos)
		playerCnt.Value += 1 -- this adds a player to the value which will be set to text
		

		if HRP.Position == hrp_Pos then
			event:FireClient(player)
			
		end
	end
    task.wait(.1)
    debounce[playerName] = nil
end)

The problem has to do with the localscript. First of all, you setup a InputBegan that is never disconnected meaning that the player can continue to press L even after leaving. Causing the number to decrease as long as they’d like. Also you need to add a check to event2 using a system similar to the post above me.

To fix this you have to disconnect the InputBegan event at some point or switch to ContextActionService:BindAction and unbind it.

EDIT*:
Heres an example of each thing:

-- localscript in StarterPlayerScripts
local ContextActionService = game:GetService("ContextActionService")

local function LeaveParty(actionName,inputState,inputObj)
	if inputState == Enum.UserInputState.Begin then
		event2:FireServer(player)
		frameKeyL.Visible = false
		ContextActionService:UnbindAction("Leave Party")
	end
end

event.OnClientEvent:Connect(function(player)
	frameKeyL.Visible = true
	ContextActionService:BindAction("Leave Party", LeaveParty, false, Enum.KeyCode.L)
end)
local debounce = {}
objChecker.Touched:Connect(function(playerHit)
	print("well")
	local playerName = playerHit.Parent.Name
	if debounce[playerName] then return end
	debounce[playerName] = true
	local player = game.Players:WaitForChild(playerName)
	local char = playerHit.Parent
	local HRP = char:FindFirstChild("HumanoidRootPart")

	if playerHit.Parent.Name == player.Name then

		HRP.Anchored = true
		HRP.CFrame = CFrame.new(hrp_Pos)
		playerCnt.Value += 1 -- this adds a player to the value which will be set to text
		

		if HRP.Position == hrp_Pos then
			event:FireClient(player)
			
		end
	end
	
end)

event2.OnServerEvent:Connect(function(player)
	if debounce[player.Name] then
		debounce[player.Name] = nil
		local char = player.Character or player.CharacterAdded:Wait()
		local HRP = char:FindFirstChild("HumanoidRootPart")
		HRP.Anchored = false
		HRP.CFrame = CFrame.new(leavePortal_GMn_1v1)
		playerCnt.Value -= 1 -- samething here
	end
end)

Im sorry for the late reply but I had no acces to my PC so…

Anyways your edited scripts worked perfectly thanks for your help!

1 Like

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