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)
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.
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.
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)
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?
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()
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.
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
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.