(HELP)Why is it doing this?

I am making an RNG game, here is part of one of the scripts,

	remotes.Combine.OnServerEvent:Connect(function(player, name1)
	
		remotes.Combine2.OnServerEvent:Connect(function(player, name2)
			local name = tostring(name1[1]).. ", ".. tostring(name2[1]).. ",".. " Brick"
			giveNewChance(player, name)
			
			print(name)
			
			
		end)
		
	end)
	
	return Table
end

for some reason the printing starts glitching like this:
image_2024-04-07_141658229

4 Likes

You have a function in a function. Disconnect the nested function once you’re done.

3 Likes

Wait what? Idk how to use that.

2 Likes
1 Like

?

	remotes.Combine.OnServerEvent:Connect(function(player, name1)
	
		remotes.Combine2.OnServerEvent:Connect(function(player, name2)
			local name = tostring(name1[1]).. ", ".. tostring(name2[1]).. ",".. " Brick"
			giveNewChance(player, name)
			
			print(name)
			
			remotes.Combine.OnServerEvent:Disconnect()
			
		end)
		
	end)
	
	return Table
end

1 Like

no disconnect the nested function

local idk
function here()
    idk = function thisisnested()
        --do stuff
        idk:Disconnect() -- can be here
    end
    -- idk:Disconnect() can be here too but u gotta make sure the function is fully executed first
end
1 Like

This?

	local connection
	remotes.Combine.OnServerEvent:Connect(function(player, name1)
	
		connection = remotes.Combine2.OnServerEvent:Connect(function(player, name2)
			local name = tostring(name1[1]).. ", ".. tostring(name2[1]).. ",".. " Brick"
			giveNewChance(player, name)
			
			print(name)
			
			connection:Disconnect()
			
		end)
		
	end)
	
	return Table
end


1 Like

Looks fine to me
Try it and check the output

1 Like

Uh oh
image_2024-04-07_143217466

1 Like

Have a print in the outer function, see how many times it’s run

1 Like

It prints one more everytime.
image_2024-04-07_143513397
(this was after 3 there is more above it)

1 Like

Your problem is probably stemming from the script firing the remote then

1 Like