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

Can anyone help with duplicating RemoteEvents?

Hello there. Today I'm working on a custom chat system. The game is using FilteringEnabled so I'm using remote events to organize responses from the client and server. The problem is that I've somehow created a loop with the RemoteEvents that outputs what I need and causes more loops. Those loops cause more loops and so on giving me more and more results I don't need. Here's all the applicable code I can think to add:

LocalScript

game.Players.LocalPlayer:WaitForChild("ClientUpdateMsgSizeEvent").OnClientEvent:connect(function(SentMsgNumber)

wait(1)

	local MsgToCheck = RegChatDisplay:WaitForChild("Message"..SentMsgNumber)
	MsgToCheck.Size = MsgToCheck.Size + UDim2.new(0, 0, 0, 18)
	
	local PreviousMsgs = RegChatDisplay:GetChildren()	
		
		for i = 1, #PreviousMsgs do
			wait(1)
			print("This should run "..#PreviousMsgs.." times.")
			PreviousMsgs[i].Position = PreviousMsgs[i].Position - UDim2.new(0, 0, 0, 18)
		end

	CheckMsgLength(SentMsgNumber)
end)

function CheckMsgLength(SentMsgNumber)

	local MsgToCheck = "Message to check"
	
	MsgToCheck = RegChatDisplay:WaitForChild("Message"..SentMsgNumber)
	wait(.1)

	if MsgToCheck.TextFits == false then
		wait(1)
	
		game.Workspace.ServerUpdateMsgSizeEvent:FireServer(SentMsgNumber)
		
	elseif MsgToCheck.TextFits == true then
		
		print("The text now fits. Do not repeat")
		
	else
		error("Somthing went wrong here")
	end
end

ServerScript

local ServerUpdateMsgSizeEvent = Instance.new("RemoteEvent")
ServerUpdateMsgSizeEvent.Name = "ServerUpdateMsgSizeEvent"
ServerUpdateMsgSizeEvent.Parent = game.Workspace

game.Players.PlayerAdded:connect(function(player)

local ClientUpdateMsgSizeEvent = Instance.new("RemoteEvent")
ClientUpdateMsgSizeEvent.Name = "ClientUpdateMsgSizeEvent"
ClientUpdateMsgSizeEvent.Parent = player

	ServerUpdateMsgSizeEvent.OnServerEvent:connect(function(Player,SentMsgNumber	)
wait(1)
		ClientUpdateMsgSizeEvent:FireClient(player,SentMsgNumber)
	end)
end)

The "wait(1)"s are just there to slow down the process and can be ignored.

Any help you guys can provide is appreciated.

1 answer

So basically, what's happening here, is that you are firing an event to the server, and then when the server recieves that, you are firing an event to the client which repeats that process and it gets itself into a loop.

I'll try and break it down with comments to help you understand:

LocalScript


game.Players.LocalPlayer:WaitForChild("ClientUpdateMsgSizeEvent").OnClientEvent:connect(function(SentMsgNumber)

wait(1)

    local MsgToCheck = RegChatDisplay:WaitForChild("Message"..SentMsgNumber)
    MsgToCheck.Size = MsgToCheck.Size + UDim2.new(0, 0, 0, 18)

    local PreviousMsgs = RegChatDisplay:GetChildren()   

        for i = 1, #PreviousMsgs do
            wait(1)
            print("This should run "..#PreviousMsgs.." times.")
            PreviousMsgs[i].Position = PreviousMsgs[i].Position - UDim2.new(0, 0, 0, 18)
        end

    CheckMsgLength(SentMsgNumber)  --When the client recieves an event, run this function
end)

function CheckMsgLength(SentMsgNumber)

    local MsgToCheck = "Message to check"

    MsgToCheck = RegChatDisplay:WaitForChild("Message"..SentMsgNumber)
    wait(.1)

    if MsgToCheck.TextFits == false then
        wait(1)

        game.Workspace.ServerUpdateMsgSizeEvent:FireServer(SentMsgNumber) --in this function you fire an event to the server

    elseif MsgToCheck.TextFits == true then

        print("The text now fits. Do not repeat")

    else
        error("Somthing went wrong here")
    end
end

ServerScript

local ServerUpdateMsgSizeEvent = Instance.new("RemoteEvent")
ServerUpdateMsgSizeEvent.Name = "ServerUpdateMsgSizeEvent"
ServerUpdateMsgSizeEvent.Parent = game.Workspace

game.Players.PlayerAdded:connect(function(player)

local ClientUpdateMsgSizeEvent = Instance.new("RemoteEvent")
ClientUpdateMsgSizeEvent.Name = "ClientUpdateMsgSizeEvent"
ClientUpdateMsgSizeEvent.Parent = player

    ServerUpdateMsgSizeEvent.OnServerEvent:connect(function(Player,SentMsgNumber    
wait(1) --when you recieve this event
        ClientUpdateMsgSizeEvent:FireClient(player,SentMsgNumber) -- you then fire the event to the player again, causing a loop!
    end)
end)

I would suggest either using 2 remotevents (one for server firing and one for client firing) or making sure you fire outside the function (or "Onevent") to get out of the loop!

I hope that helps, if you're still confused or need help, reply! If not, click "Accept Answer" :)

1 comment

0

I guess I'm still rather confused. I'd like the loop to continue until the TextFits statement returns true, but that's not happening. The loop doesn't stop once TextFits == true. I am using two remote events for server and client-side operations. Could you elaborate some more? Thanks

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