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

Would someone guide me in the right direction? - remote event updates

longtime lurker here, first post!

Im Working on a wave defense game and would like to have the gui keep players updated as to whats going on. My attempt works for the first cycle and then stays displaying the last update.

My question is how can I approach the gui updates to be continuous during gameplay?

I have 3 remote events in replicated storage [StartMob, Survive, EndMob]

in serverscriptservice, I have a simple while loop for the game enemy spawner in the game manager

all times are short for testing

local creator = require(game.ReplicatedStorage.Creator)
local MOB = game.ServerStorage.Enemy


while true do
	
wait(10)
creator.make("Enemy")
	
	
end

for the module, the routine [sound, visual cue, spawn enemy, clear all enemies] works correctly

local Creator = {} -- Create our module

function Creator.make(part) 
	local startmob = game.ReplicatedStorage.StartMob
	local endmob = game.ReplicatedStorage.EndMob
	local survive = game.ReplicatedStorage.Survive

	-- fire the remote event to update players gui to prepare
	startmob.OnServerEvent:connect(function(player)
		startmob:FireClient(player, "The Next Wave Is Coming")
	end)
	
	--create sound in sound service--
	local sound = game.ServerStorage:FindFirstChild("siren")
	local snd = game.ServerStorage:FindFirstChild(sound)
	local sndcopy = sound:Clone()
	sndcopy.Parent = game.SoundService
	wait(10)
	sndcopy:Destroy() --destroy sound
	
	-- just for show effect
	local beam = game.Workspace.Beam
	beam.Transparency = 0
	
	--fire remote for wave start
	survive.OnServerEvent:connect(function(player)
		survive:FireClient(player, "They're Here!")
	end)
	--create a clone from storage
	local origin = game.ServerStorage:FindFirstChild(part)
	local copy = origin:Clone()
	copy.Parent = game.Workspace
	--time for the clone to do its thing
	wait(5)
	
	--remove the effect
	beam.Transparency = 1
	copy:Destroy() --destroy the clone
	
	--fire remote for end of wave
	endmob.OnServerEvent:connect(function(player)
		endmob:FireClient(player, "We're Safe...For Now")
	end)
end
return Creator

in a local script in the gui: (i have gui in startergui)

local gui = script.Parent.Frame.TextLabel
local survive = game.ReplicatedStorage.Survive
local endmob = game.ReplicatedStorage.EndMob
local startmob = game.ReplicatedStorage.StartMob


startmob.OnClientEvent:connect(function(text)
	gui.Text = text
end)

startmob:FireServer()



survive.OnClientEvent:connect(function(text)
	gui.Text = text
end)

survive:FireServer()


endmob.OnClientEvent:connect(function(text)
	gui.Text = text
end)


endmob:FireServer()


my result is the three updates to the gui occur. im left with were safe for now even when the next wave is ready.

any help would be appreciated!

i checked in the dev articles and educational resources from the roblox pages but either im just stuck or just overthinking this.

1 comment

0

You should keep your event connections outside of the while loop unless you disconnect them afterwards, this will cause memory leak/lag later on.

2 answers

✓ Accepted answer

Your code is horrible and you need to rewrite it. Here's how I would have done it:

-- server script
while true do
	RemoteEvent:FireAllClients("The wave is beginning!")
	playsound()
	RemoteEvent:FireAllClients("They're here!")
	createZombies()
	wait(n) -- round time
	RemoteEvent:FireAllCliens("Phew, we're safe for now.")
end

-- local script
RemoteEvent.OnServerEvent:Connect(function(message)
	TextLabel.Text = message
end)

Never connect functions inside a while loop unless you are disconnecting them inside the same iteration.

Your local scripts in this case should only recieve messages, it should never fire to the server. The server should instead be firing to the clients whenever you want it to in the loop.

In your local script even though the three remote events are fired, the last :FireServer() is the only one that shows because the other three are overwritten.

@ radiant_Light203

Your code is horrible and you need to rewrite it.

I was able to clean up code following the format of your example.

Never connect functions inside a while loop unless you are disconnecting them inside the same iteration.

I removed all connections that were unnecessary.

Also made my module functions only do what they need to, leaving the game loop to do the rest.

Your local scripts in this case should only recieve messages, it should never fire to the server.

The :FireServer in the listener LocalScript was removed (all of them) and was formatted to:

 local RemoteEvent = game.ReplicatedStorage.remoteEvent
 local TextLabel = script.Parent.Frame.TextLabel
 
 RemoteEvent.OnClientEvent:Connect(function(message)
 TextLabel.Text = message
 end)
 

when i was making changes to the LocalScript, studio gave an error for the OnServerEvent in your example, stating that only the server could initiate this so i changed it to the client - OnClientEvent which worked perfectly!

thanks for the advice, really!

This gave me a more hands on understanding to remote events for the gui... now to play with other ideas in studio with that in mind

2 comments

0

Have you any prior experience with scripting? You seem to know your jargon well and are competent enough to learn from vague instructions.

0

Not Much with Lua, Like i mentioned I really have been browsing the site for a while now, i usually read articles on here or on the roblox dev and education site. As for the Vague Instructions? I feel that was enough to help with my question. I know a lot of people try to ask for full scripts and such but there is no benefit there. Once i get this running, ill def send you link to check it out :)

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