Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix gui stacks?

I am making a chat gui and everything works perfectly but when 2 players spam the chat can overlap? Any way to fix this?

print("New Server")
--Variables--
Settings = {
	Enabled = true --Chat Enabled--
	,ServerResponseColor = "Cyan" --Server Text--
	,PlayerChatColor = "White" --Color players text is--
	,ShowRankInGroup = {false, 7} --Example Goldgrilz7 [Leader]: Hey guys!--
	,Admins = {"Goldgrilz7", 7}
}

--Only change anything below this line if you know what you're doing--

function cleantable() --Wipes clean all chat history auto updates players--
print("Cleaning Table")
gui = script.Chat.Chat:GetChildren()
for i = 1, #gui do
	if gui[i].Name == "Chat_Bar"
		then
		gui[i]:remove()
	end
	for i, v in pairs(game.Players:GetChildren()) do
	if v.PlayerGui:FindFirstChild("Chat") ~= nil
		then
		v.PlayerGui:FindFirstChild("Chat"):remove()
	end
	end
end
end

cleantable()

if Settings.Enabled == false
	then
	script.Chat.HideChat.Disabled = true
end

function ac(player) --Admin check
	a = false
	for i = 1, #Settings.Admins do
		if Settings.Admins[i] == player
			then
			a = true
		end
	end
	return a
end

if Settings.Enabled == true
	then
	script.Chat.HideChat.Disabled = true
	
function onchat(msg, player)
	wait(.05)
		admincheck = ac(player.Name)
	if msg:sub(1,3):lower() == ";sm" and admincheck == true
		then
		_G.respond(msg:sub(5))
	else if msg:sub(1,10):lower() == ";showranks" and msg:sub(11,13) ~= "off" and admincheck == true
		then
		Settings.ShowRankInGroup[1] = true
		_G.respond("Enabled Visual Chat Rank")
		else if msg:sub(1,12):lower() == ";showranksoff" and admincheck == true
		then
		Settings.ShowRankInGroup[1] = false
		_G.respond("Disabled Visual Chat Rank")
		else if msg:sub(1,8):lower() == ";groupid" and admincheck == true
		then
		Settings.ShowRankInGroup[2] = tonumber(msg:sub(9))
		_G.respond("Changed Group To"..msg:sub(9))
	else
	print(msg, player)
	p = game.Players:GetChildren()
	gui = script.Chat
		gui.Chat.Update.Disabled = false
		message = script.Chat_Bar:Clone()
		message.Text = player.Name..": "..msg
		message.TextColor3 = BrickColor.new(Settings.PlayerChatColor).Color
		if Settings.ShowRankInGroup[1] == true
				then
				message.Text = player.Name.." ["..player:GetRoleInGroup(Settings.ShowRankInGroup[2]).."]: "..msg
				end
		message.Parent = gui.Chat
		game:GetService("Debris"):AddItem(message, 120)
	for i = 1, #p do
		wait(.01)
		if p[i]:FindFirstChild("PlayerGui") ~= nil
			then
			if p[i].PlayerGui:FindFirstChild("Chat") ~= nil
				then
				p[i].PlayerGui.Chat.Chat.LUpdate.Disabled = false
			message = script.Chat_Bar:Clone()
			message.TextColor3 = BrickColor.new(Settings.PlayerChatColor).Color
			message.Text = player.Name..": "..msg
			if Settings.ShowRankInGroup[1] == true
				then
				message.Text = player.Name.." ["..player:GetRoleInGroup(Settings.ShowRankInGroup[2]).."]: "..msg
				end
			game:GetService("Debris"):AddItem(message, 120)
			message.Parent = p[i].PlayerGui.Chat.Chat
			message.TextTransparency = 1
			message.TextStrokeTransparency = 1
			end
		end
	end
	end
		end
	end
	end
end







game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg) onchat(msg, player) end)
	_G.respond(player.Name.." Has Joined")
end)

while true do
	wait()
	for i, v in pairs(game.Players:GetChildren()) do
	if v.PlayerGui:FindFirstChild("Chat") == nil
		then
		clone = script.Chat:Clone()
		clone.Parent = v.PlayerGui
	end
	end
end
end

2 answers

✓ Accepted answer

Well, I made a chat once and I had the same problem. To fix it, I made a debounce. It's something like this:

local ready = true --the debounce

function chat() --function that adds messages to the chat
	ready = false
	--chat code
	ready = true
end

player.Chatted:connect(function() --you can use a named function, but I'm using an anonymous function
	repeat wait(0) until ready --a repeat loop; if ready (the debounce) is false, it will keep waiting one frame (wait(0)) until ready is true again
	chat() --call your chat function
end)

So basically, the ready variable will always be true until the chat function is called. ready will remain false until the call function finishes.

It might be different for your chat, but that is how I prevented 'chat-overlap' in my custom chat.

Hoped I helped.

~coo1o

If you are wanting to make it so when there is one gui already open, and someone trys to reopen it the current gui goes away and the new one appears, here is the solution


Name = "GUINAMEINHERE" if player.PlayerGui:FindFirstChild(Name) then game.Debris:AddItem(player.PlayerGui[Name],0) end

This discussion is preserved as it was. New answers, comments, and votes are closed.