Remote event sometimes works and sometimes doesnt?

Im trying to fire a remote event from a module script that is located in server storage, the local script recieves the event and applies lighting settings. For some reason sometimes the remote event doesnt work and stops recieving any data.
I have tried putting the local script in replicated first, I disabled reset on spawn in gui but it doesnt seem to help
Heres the code and some images

local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")

local function ApplyLighting(LightingFolder:Folder)

	local DefaultValuesFolder = LightingFolder:FindFirstChild("DefaultValues")
	local EffectsFolder = LightingFolder:FindFirstChild("Effects")

	for Index, Part in Lighting:GetDescendants() do

		local Tags = Part:GetTags()
		if table.find(Tags, "Temporary") then

			Part:Destroy()

		end

	end
	for Index, LightingValue in DefaultValuesFolder:GetChildren() do

		Lighting[LightingValue.Name] = LightingValue.Value

	end
	for Index, Effect:ColorCorrectionEffect in EffectsFolder:GetChildren() do

		local EffectClone = Effect:Clone()
		EffectClone:AddTag("Temporary")
		EffectClone.Parent = Lighting

	end

end

ApplyLighting(workspace.LobbyFolder.Lighting)
RemoteEvents:WaitForChild("ApplyLighting").OnClientEvent:Connect(function(Folder:Folder)
	
	print("ApplyLighting")
	ApplyLighting(Folder)
	
end)


изображение

1 Like

I dont know if its just a studio problem or not

Your local script may not be connecting to the remote event until after it’s been fired by the server.

Where/when is the remote event being fired from the server?
Like the server script that must be requiring the module script you fire it from and calling whatever function fires it.

You can check by adding prints right before the event is fired from the server and prints right before the RemoteEvents:WaitForChild("ApplyLighting").OnClientEvent:Connect in your local script.

1 Like

Thats what I did but it still doesnt work sometimes.
It feels random sometimes it works and sometimes it doesnt

RemoteEvents:WaitForChild("ApplyLighting").OnClientEvent:Connect(function(Folder:Folder)
	
	print("ApplyLighting")
	ApplyLighting(Folder)
	
end)

oh sorry missunderstood the comment, let me try that

Heres the result
When doesnt work and when it works
изображение
изображение

How about adding some delay before firing the event?

can I see server sided script?

Hmm ,and where is the print in the localscript? Is it right before you connect to the Remote Event? If it’s elsewhere in the script it’s possible a :WaitForChild is still causing the :Connect to run after the server script has already fired the event.

Everything you’re seeing, especially the randomness, is typical of race conditions where the scripts load/run in different orders.

You can try a wait before firing from the server script, or you can use like a RemoteFunction from the client to fetch the lighting folder from the server.

The thing is the server side already has a big delay before firing the event so the remote event would definetly get loaded

So if you’re saying it’s not the difference/order of the client first connecting to the remote event vs. when it’s fired could it be how it’s being fired?
Are you doing :FireAllClients or :FireClient on a specific Player?

fireclient on a specific player

Oh my god I feel so dumb
Apperantly there were two remote events with the same name.
After removing one the script started working again

1 Like

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