Wont apply weather on player join

So I have this code and for some reason when a player joins they still dont have it applied

local RainModule = require(game.ReplicatedStorage.Weather.Rain)
local Init = require(game.ReplicatedStorage.Weather.Screendroplets.Init)

local Lighting = game.Lighting
local Terrain = game.Workspace.Terrain
local CollectionService = game.CollectionService
local TweenService = game.TweenService
--local WindService = require(game.ReplicatedStorage.Weather.WindService)
local Settings = {}

--local newWind = WindService.new(Settings)

-- | Terrian Local | ---

local Atmosphere = Lighting:WaitForChild("Atmosphere");
local Ambient = Lighting.Ambient
local OutdoorAmbient = Lighting.Ambient
local Brightnesss = Lighting.Brightness
local ColorShift_Top = Lighting.Ambient
local ColorShift_Bottom = Lighting.Ambient
local Clouds = Terrain:WaitForChild("Clouds");
local WindShake = require(script.WindShake)

-- All locals / settings for windshake and requires lines -- 


local WIND_DIRECTION = Vector3.new(1, 0, 0.3)
local WIND_SPEED = 20
local WIND_POWER = 4
local SHAKE_DISTANCE = 150


local THUNDER_DIRECTION = Vector3.new(1, 0, 0.3)
local THUNDER_SPEED = 28
local THUNDER_POWER = 6
local THUNDER_DISTANCE = 300

local WindLines = require(script.WindShake)


-- Lighting Storage

local SoundOne = script.ThunderStuff.One
local SoundTwo = script.ThunderStuff.Two
local ThunderSounds = {SoundOne, SoundTwo}
local Thunder = false


---Functions----

local currentWeather = nil

local function tweenAtmosphereDensity(targetDensity)
	local tweenInfo = TweenInfo.new(
		7, -- Duration (change this value to control the speed of the tween)
		Enum.EasingStyle.Linear, -- Easing style (you can choose any style you prefer)
		Enum.EasingDirection.In -- Easing direction
	)
	local tweenGoal = {Density = targetDensity}
	local tween = TweenService:Create(Atmosphere, tweenInfo, tweenGoal)
	tween:Play()
end



---IF we swaped to overture this can be much more secure----
local function applyWeather(weather)
	if weather == "Raining" then
		Init:Enable()
		RainModule:Enable()
		WindLines:Cleanup()
		WindShake:Cleanup()
		tweenAtmosphereDensity(0.30)
	elseif weather == "Stormy" then
		Init:Enable()
		RainModule:Enable()
		tweenAtmosphereDensity(0.418)

		-- Inits WindLines and Windshake for foliage

		WindLines:Init({
			Direction = WIND_DIRECTION,
			Speed = WIND_SPEED,
			Lifetime = 1.5,
			SpawnRate = 11,
		})

		WindShake:SetDefaultSettings({
			WindSpeed = WIND_SPEED,
			WindDirection = WIND_DIRECTION,
			WindPower = WIND_POWER,
		})

		WindShake:Init({
			MatchWorkspaceWind = true,
		})
	elseif weather == "Thunder Storm" then
		Thunder = true
		Init:Enable()
		RainModule:Enable()
		tweenAtmosphereDensity(0.500)

		-- Inits WindLines and Windshake for foliage

		WindLines:Init({
			Direction = WIND_DIRECTION,
			Speed = WIND_SPEED,
			Lifetime = 3,
			SpawnRate = 14,
		})

		WindShake:SetDefaultSettings({
			WindSpeed = THUNDER_SPEED,
			WindDirection = THUNDER_DIRECTION,
			WindPower = THUNDER_POWER,
		})

		WindShake:Init({
			MatchWorkspaceWind = true,
		})

		-- Sets up random sounds
		while Thunder do 
			wait(math.random(10,15))
			ThunderSounds[math.random(1, #ThunderSounds)]:Play()
		end
	elseif weather == "Foggy" then
		
		tweenAtmosphereDensity(0.418)
		WindLines:Cleanup()
		WindShake:Cleanup()

	elseif weather == "Clear" then
		
		Init:Disable()
		Thunder = false
		RainModule:Disable()
		tweenAtmosphereDensity(0.276)

		WindLines:Cleanup()
		WindShake:Cleanup()
	end
end

game.Players.PlayerAdded:Connect(function()
	applyWeather(game.ReplicatedStorage:GetAttribute("weather"))
end)

game.ReplicatedStorage.cmdrhelper.weather.WeatherComms.OnClientEvent:Connect(function(weather)
	applyWeather(weather)
end)

Probably because thats a local script.

When the player join, that event game.Players.PlayerAdded is not created yet, so, the player joined. The local script is created and given to player, now the lines exist, but the PlayerAdded event didnt fired for that player cause it already fired and the event connection was not listening yet cause didnt existed, it will fire for future players joining.

If you want to run that applyWeather() function, simply do it at the end of the script withtout adding the PlayerAdded. Once the script is created and runs, it will make the function to run.

This at the bottom of the script

applyWeather(game.ReplicatedStorage:GetAttribute("weather"))
end)

Omg thank you, silly oversight

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