Issue with Weather System

I recently began building a weather system from scratch. It operates by using server sided script as its foundational code which uses math.random() to determine whether it should rain or not with FireAllClients() which sends a signal to the local script to enable the raining effect for all players on the server. The server script still functions and I recieved no issues when I ran it in Studio, however when I played the test with two players, neither of them was able to see the rain effect. I will provide you with further scripts.

Server script:

local Rain = coroutine.wrap(function()
	
	local Every = 1
	local Raining = false
	local Cooldown = false
	
	while wait(Every) do
		
		local Chance = math.random(1,1)
		
		if Chance == 1 and Raining == false and Cooldown == false then
			
			Raining = true
			
			local Duration = math.random(180, 360)
			
			warn("Raining")
			
			game.Workspace.Weather.Raining.Value = true

			game.ReplicatedStorage.Remotes.Weather.Rain:FireAllClients("Start Raining")
			
			spawn(function() -- Duration
				
				wait(Duration)
				
				Cooldown = true
				Raining = false
				
				spawn(function()
					
					wait(300)
					
					Cooldown = false
					
				end)
				
				game.ReplicatedStorage.Remotes.Weather.Rain:FireAllClients("Stop Raining")
				
				game.Workspace.Weather.Raining.Value = false
				
				warn("Rain End")
				
			end)	
			
		end
		
	end
	
end)

Rain()

Local Script:

task.wait(2)
local Rain = require(script.Rain)

Rain:SetColor(Color3.fromRGB(script.Color.Value.x, script.Color.Value.y, script.Color.Value.z))
Rain:SetDirection(script.Direction.Value)

Rain:SetTransparency(script.Transparency.Value)
Rain:SetSpeedRatio(script.SpeedRatio.Value)
Rain:SetIntensityRatio(script.IntensityRatio.Value)
Rain:SetLightInfluence(script.LightInfluence.Value)
Rain:SetLightEmission(script.LightEmission.Value)

Rain:SetVolume(script.Volume.Value)

Rain:SetSoundId(script.SoundId.Value)
Rain:SetStraightTexture(script.StraightTexture.Value)
Rain:SetTopDownTexture(script.TopDownTexture.Value)
Rain:SetSplashTexture(script.SplashTexture.Value)

local threshold = script.TransparencyThreshold.Value
if script.TransparencyConstraint.Value and script.CanCollideConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.Transparency <= threshold and p.CanCollide
		end
	)
elseif script.TransparencyConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.Transparency <= threshold
		end
	)
elseif script.CanCollideConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.CanCollide
		end
	)
end

game.ReplicatedStorage.Remotes.Weather.Rain.OnClientEvent:Connect(function(Action)
	if Action == "Start Raining" then
		warn("RAIN STARTED")

		Rain:Enable()

	elseif Action == "Stop Raining" then

		Rain:Disable()

	end
end)

and there’s the Module script which simulates the rain effect. I will provide you with it if needed.

1 Like

You forgot to account for if a player joins after the rain already started.

Fixed code: (server script)

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Loops
task.spawn(function()
	local Every = 1
	local Raining = false
	local Cooldown = false
	local Connections = {}

	while task.wait(Every) do
		local Chance = Random.new():NextInteger(1, 1)

		if Chance == 1 and not Raining and not Cooldown then
			Raining = true

			local Duration = Random.new():NextInteger(180, 360)

			warn("Raining")

			workspace.Weather.Raining.Value = true
			ReplicatedStorage.Remotes.Weather.Rain:FireAllClients("Start Raining")
			
			Connections.PlayerAdded = Players.PlayerAdded:Connect(function(player)
				ReplicatedStorage.Remotes.Weather.Rain:FireClient(player, "Start Raining")
			end)
			
			task.delay(Duration, function()
				Cooldown = true
				Raining = false
				
				task.delay(300, function()
					Cooldown = false
				end)
				
				Connections.PlayerAdded:Disconnect()
				Connections.PlayerAdded = nil
				
				ReplicatedStorage.Remotes.Weather.Rain:FireAllClients("Stop Raining")
				workspace.Weather.Raining.Value = false
				warn("Rain End")
			end)	
		end
	end
end)
2 Likes

Thank you, I’ve spent so long and who would’ve known it’s as simple as this. Normally I would notice a mistake like this one but oh well. Thanks again.

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