OnServerEvent fires multiple times

Hi,
I’m using OnServerEvent event to teleport player but when new :FireServer() is called it runs +1 time more just like that:

remoteEvent.OnServerEvent:Connect(function(character)
    print(character)
end)

Output:

Suchareczek6656 -- :FireServer() fired
Suchareczek6656 -- :FireServer() fired
Suchareczek6656
Suchareczek6656 -- :FireServer() fired
Suchareczek6656
Suchareczek6656
Suchareczek6656 -- :FireServer() fired
Suchareczek6656
Suchareczek6656
Suchareczek6656
Suchareczek6656 -- :FireServer() fired
Suchareczek6656
Suchareczek6656
Suchareczek6656
Suchareczek6656

How can I fix it?

Could you show the call script as well? Although you could put a debounce on it.

I’m guessing you are using a touched event, right? If so, use a debounce:

local debounce = true
--Call the function (ie. script.Parent.Touched:Connect(function(hit)
   if debounce then
      debounce = false
      --rest of script
      task.wait(1)
      debounce = true
   end
end) --close the connected function

You might need to use a bigger value than 1

I’m using ProximityPropomt.Triggered

Still use what he said, works for both.

1 Like

Either way, debounce is the best thing to do.

But what if someone trigger it 200 times? 200 events at the same time would lag the game

Why would it? The debounce will just stop the remote event from firing

Debounce will only stop the code after if statement

If you don’t add the debounce it will be even worse

I’d rather to know how it happen than know how to fix it

Just ignore that

That’s not proximity prompt fault

Hey there.

I looked at your output and I am almost certain the problem is that you sre not disconnecting your event.

If you are using an event INSIDE another event make sure you disconnect the event after you have done with it or else you eill have multiple connections cennected to the same event.

Could we see the scripts as well as the nested events

ServerScriptService:

local GetCharacter = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("GetCharacter")

local function Teleport(player, position)
	GetCharacter.OnServerEvent:Connect(function(character)
		print(character)
	end)
	GetCharacter:FireClient(player)
end

workspace.HJ.PPPart.Attachment.ProximityPrompt.Triggered:Connect(function(p)
	Teleport(p, Vector3.new()) -- this is test
end)

StarterPlayerScripts:

local GetCharacter = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("GetCharacter")

GetCharacter.OnClientEvent:Connect(function()
	local character = game:GetService("Players").LocalPlayer.Character
	GetCharacter:FireServer(character)
end)

As I suspected, you have a OnServerEvent insid the Proximity.Triggred event.

The structure of the code is the problem. There are a few things to mention too.

local GetCharacter = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("GetCharacter")

GetCharacter.OnClientEvent:Connect(function()
	local character = game:GetService("Players").LocalPlayer.Character
	GetCharacter:FireServer(character)
end)

This is pointless as getting the character can be done on the server.
This will bring up a security issue as the client can send whatver they want.
You should have used a Remote function for two way communication.


Now if you want to teleport the player it is as easy as this.

-- ServerScript

local Triggered(Player: Player)
    local Character = Player.Character -- Character should always exist if the player is using proximity prompt
    local Humanoid = Character:FindFirstChild("Humanoid")
    if not Humanoid or Humanoid.Health < 1 then return end
    Character:SetPrimaryPartCFrame(CFrame.new(TELEPORT_POSITION))-- His is the runcion you can use to teleport a character.
end

ProximityPrompt.Triggered:Connect(Triggered)

Thre is no need for cross device communication in this case. it can and should be done on the server.

Also disconnect your vnts after you have finished using them in the future. Not disconnecting events will cause lag if you have a lot of them and you will run into bugs like that one.

You can’t get the character from player using server side

Yes you can, there are multiple ways to do it; such as game.Workspace[Player.Name]. Also, @koziahss is right.