I am making a game with storm systems in it. I have made a script that detects if a player is in the storm. Once the player is in the area it starts raining on their end. When they are out it stops raining, the problem is if there is more than 1 storm, the scripts cancel each other out.
How would I go about fixing this?
Server Script
brick = script.Parent
range = 3000
while true do
wait(.1)
plrs = game.Players:GetChildren()
for i,plr in ipairs(plrs) do
if plr.Character ~= nil then
tor = plr.Character.UpperTorso
if (brick.Position-tor.Position).magnitude <= range then
game.ReplicatedStorage.Rain:FireClient(plr)
else
game.ReplicatedStorage.NoRain:FireClient(plr)
end
end
end
end
Well idk if this is the most efficient way or not but the way I would do: Not making multiple server scripts. Instead make only one script and loop through the table of tornadoes. Make a bool value that is false, false = player is not in the storm. And when looping the through the table, if the bool value is true(the player is in the storm area) then do stuff.
Edit: If this is kinda confusing, I’m sorry. Here’s how it looks like in code:
local boolValue
local tornadoes = {} -- your table of tornadoes
while true do
boolValue = false
for i,v in pairs(tornadoes) do
for i,plr in ipairs(plrs) do
if plr.Character ~= nil then
tor = plr.Character.UpperTorso
if ((brick.Position-tor.Position).magnitude <= range) and (boolValue == false) then
boolValue = true
game.ReplicatedStorage.Rain:FireClient(plr)
end
if boolValue == false then
game.ReplicatedStorage.NoRain:FireClient(plr)
end
end
end
end
wait(waitTime)
end
I agree with not having multiple storm scripts, but going along with that, every time you want to check if the player is in the storm, have a boolean value (true/false) be initialized as false, and if you find them to be in the storm, set the boolean value to true. Do not set it to false in the for loop which iterates between all the storms, as this will cause the same issue you initially described.
Just put it in the in pairs loop that loop through the players. Now I just realize, this is not good cause it will fires the same event multiple times.
EDIT: Nvm just add a and boolValue = true/false to the if statement.