Sometimes it works, sometimes it doesn't

Hi, I was scripting some code that when you enter a two users with a string, sometimes the code works but reaches a point where it stops working and does not join them, no error message or anything, is there any workaround or solution to this?

local debounce = false

local points = 0


game:GetService("Players").PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(character)
		
		points = points + 1
		if #game:GetService("Players"):GetChildren() == 2 then
			if not debounce then
				debounce = true	
			
				local players_select = game:GetService("Players"):GetPlayers()
				local cuerda = Instance.new("RopeConstraint")
				
				local selectplayer = players_select[math.random(1, #players_select)]
				
				local Characterrandomplayer = selectplayer.Character or selectplayer.CharacterAdded:Wait()
				
				print("Player random: "..selectplayer.Name)
				cuerda.Visible = true
				cuerda.Parent = Characterrandomplayer:WaitForChild("HumanoidRootPart")
				cuerda.Attachment0 = Characterrandomplayer:WaitForChild("HumanoidRootPart"):FindFirstChild("RootAttachment")
				wait()
				
				for _,jugadores in pairs(game:GetService("Players"):GetChildren()) do
					if jugadores.Name ~= selectplayer.Name then
						
						local CharacterJugador = jugadores.Character or jugadores.CharacterAdded:Wait()
						
						cuerda.Attachment1 = CharacterJugador:WaitForChild("HumanoidRootPart"):FindFirstChild("RootAttachment")
					end
				end
				
				cuerda.Length = 10
				local HighLishtClone = script.Highlight
				HighLishtClone.Parent = selectplayer.Character
				HighLishtClone.Enabled = true
				debounce = false
			end 
		end
		
	end)
end)

Code update:
An artificial intelligence solved the code for me, it is very effective!

Here solution:

Resumen

There are a few things that could be causing problems in your code, here are some suggestions that might help you fix the errors:

Debounce seems to be being used to prevent the string from being created multiple times. However, if there is an error in the process of creating the string, debounce will remain true and you will not be able to create the string on future attempts. To work around this, it is advisable to set a timer after debounce is set to true, so that after a specified time, debounce is set to false again. For example, you can use wait(5) to set a timeout of 5 seconds.

It is possible that the reason your code sometimes works and not others is because it is trying to create the string before the character is fully loaded. To avoid this, make sure the character is fully loaded before creating the string. You can do this by using the WaitForChild method on the player’s Character object.

It is also possible that you are trying to access player characters before they have joined the game, which could cause errors. To avoid this, it is advisable to use wait() or the CharacterAdded event to ensure that the player has been fully added to the game before attempting to access their character.

With these changes, the updated code would look like this:

Code Working:

Resumen
local debounce = false
local points = 0

game:GetService("Players").PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        points = points + 1
        if #game:GetService("Players"):GetChildren() == 2 then
            if not debounce then
                debounce = true
                wait(5) -- Establecer un tiempo de espera antes de establecer debounce a false
                debounce = false

                local players_select = game:GetService("Players"):GetPlayers()
                local cuerda = Instance.new("RopeConstraint")

                local selectplayer = players_select[math.random(1, #players_select)]
                local Characterrandomplayer = selectplayer.Character or selectplayer.CharacterAdded:Wait()
                Characterrandomplayer:WaitForChild("HumanoidRootPart")

                cuerda.Visible = true
                cuerda.Parent = Characterrandomplayer:WaitForChild("HumanoidRootPart")
                cuerda.Attachment0 = Characterrandomplayer:WaitForChild("HumanoidRootPart"):FindFirstChild("RootAttachment")

                for _,jugadores in pairs(game:GetService("Players"):GetChildren()) do
                    if jugadores.Name ~= selectplayer.Name then
                        local CharacterJugador = jugadores.Character or jugadores.CharacterAdded:Wait()
                        CharacterJugador:WaitForChild("HumanoidRootPart")
                        cuerda.Attachment1 = CharacterJugador:WaitForChild("HumanoidRootPart"):FindFirstChild("RootAttachment")
                    end
                end

                cuerda.Length = 10
                local HighLishtClone = script.Highlight
                HighLishtClone.Parent = selectplayer.Character
                HighLishtClone.Enabled = true
            end 
        end
    end)
end)

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