Function code happens multiple times

Hello, Developers.

I am currently experiencing an issue for a Round-based game I’m working on.

Basically, I’m making so in the script that controls the round, It detects when a character respawns via CharacterAdded, And then teleports them to a random spawn block in the current map, and gives them a weapon, This works just fine. But in the 2nd round, the character is teleported twice, and given 2 weapons. 3rd round, teleported 3 times, given 3 weapons, you get the deal. Anyways, Im unsure how to fix this, Please note that i can not make respawn detecting through the client, As a lot of changes would be made, and the way im doing it right now is fine.

Server script (simplified):

 function RoundonGoing()
	 print("Running round")
	 for _, player in pairs(game.Players:GetPlayers()) do
		 if player.Character then
			 player.CharacterAdded:Connect(function(character)
				 wait()
				 if character.Parent ~= workspace then return end
				 if map then
					 if map:FindFirstChild("Spawns") then
						 local spawns = map:WaitForChild("Spawns"):GetChildren()
						 game.ReplicatedStorage.Teleport:FireClient(player, spawns)
						

					 end
				 end
			 end)
		 end
	 end
end

Local script that detects the event

game.ReplicatedStorage.Teleport.OnClientEvent:Connect(function(spawns,)
	if plr then
		print("spawned")

		local randomIndex = math.random(1, #spawns)
		char.HumanoidRootPart.CFrame = spawns[randomIndex].CFrame
	end
end)
7 Likes

to make a code block (and save many scripters’ eyes)
just do three or more ` (backquote) before and after the script
example (``` or `````)

`` (make this three, not two)
print(“Hello!”)
` (make this three, not one)

2 Likes

Okay, thanks, but im more looking for a solution on my problem rather than how to use this website.

3 Likes

ok but please edit your message cuz nobody would bother to help with that

2 Likes

you mean no one would care to help me just because i didnt put it in a code block? thats a bit rude, you know. i’d prefer to get answers rather than someone telling me what to do with my message.

3 Likes

im not trying to be rude, im just trying to help you out
it makes it alot easier to modify the code or even analyse it

1 Like

i know, i already edited, so you dont have to keep responding (not saying this in a rude way) i dont wanna have this topic full of replies that dont even have to do with the main subject.

2 Likes

it might be this.
you’re checking for the player’s character and if they have one, wait for their next respawn

2 Likes

also i dont see any reason to send a message to the client to teleport, this should be done via server

2 Likes

if its done via server, people with bad wifi wont be teleported, thats an issue i had a long time ago, using RemoteEvents and such fixes it.

2 Likes

interesting reason, okay checks out i guess

try this

function RoundonGoing()
   print("Running round")
   for _, player in game.Players:GetPlayers() do
      if player.Character then
         task.wait()
	     if map then
		    if map:FindFirstChild("Spawns") then
			   local spawns = map:WaitForChild("Spawns")
			   game.ReplicatedStorage.Teleport:FireClient(player, spawns)
		    end
         end
      end
   end
end
game.ReplicatedStorage.Teleport.OnClientEvent:Connect(function(spawns)
    print("spawned")	
    local randomIndex = math.random(#spawns:GetChildren())
    local spawn = spawns:GetChildren()[randomIndex]
	game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = spawn.CFrame
end)
1 Like

If you aren’t disconnecting any of the Player.CharacterAdded events after each round or cycle then you’ll keep accumulating those connections.

Which is why that event keeps firing multiple times

3 Likes

i’ve ran into something similar, wherever you’re casting this function, you’re probably just refiring the connection to run again. ex:

Fire on Click:
(connection 1)
~Server / Game Connects to Object again, not disabling previous connection.
Fire On Click
(connect 1)
(connect 1)

Possible Solution:
whatever you’re setting it, set it to nil in between rounds, its prob needs a disconnect in between

3 Likes

I see, i thought something like that would be the problem. How would i got about disconnecting the CharacterAdded event? is there like a “Disconnect()” function i should use? i saw people talk about that, but im not sure in what part of my code do i disconnect it, do i disconnect it after the connection?

3 Likes
local Connection
Connection = player.CharacterAdded:Connect(function(char)
       --Do stuff with the character
end)

--When you don't need the connection anymore, call :Disconnect()
Connection:Disconnect()
4 Likes

you can also utilise :Once

player.CharacterAdded:Once(function(character)

end)

it’s basically connect but disconnects immediately after it is fired

3 Likes

I bet that’s a quick solution for his current problem lowkey, especially if you’re instanting too many connections. Sounds like it via the initial post.

2 Likes

a simpler solution would be to track the current characters

local characters = {}

game.Players.PlayerAdded:Connect(function(player)
    characters[player.Name] = ""
    player.CharacterAdded:Connect(function(character)
        characters[player.Name] = character
    end)
end)

function RoundonGoing()
   print("Running round")
   for playername, character in characters do
      if character == "" then continue end
      task.wait()
	  if map then
		 if map:FindFirstChild("Spawns") then
			local spawns = map:WaitForChild("Spawns")
			game.ReplicatedStorage.Teleport:FireClient(game.Players:FindFirstChild(playername), spawns)
         end
      end
   end
end
2 Likes

It seems to work once, If the character respawns, and then respawns once again a bit after, it wont teleport them back, very odd. i tried the same code but replaced “once” with “connect” and it went back to normal, but obviously did the thing im trying to prevent.

1 Like

wait, you’re trying to respawn them back into the match?

3 Likes