Skip over something in script if its already happening

Hello,

I currently have a script that holds a remote event for an attack skill im making. In the skill it created dark fog through the lighting and after the skill finishes, returns it back to the normal lighting. This script is server-sided as I want everyone to be able to witness it.

The problem that im facing is that when more than one person uses this skill, it messes up the process of the dark fog closing in on the player. Ive tried multiple things including bools and while loops but couldn’t seem to figure it out on my own. Here is the script for the event along with a representation of what happens when the skill is activated.

In the script below you will find a note where my problem is at:

se.OnServerEvent:Connect(function(Player)

	local atmos = game.Lighting
	local shrimp = rs:WaitForChild("Shrimp"):Clone()
	local fire = rs:WaitForChild("Attacks").CPart.Attachment
	local fire1 = fire.ParticleEmitter:Clone()
	local fire2 = fire.Spike:Clone()
	local firesound = rs:WaitForChild("AttSounds").ShrimpAttack
	local opera = rs:WaitForChild("AttSounds").opera

	atmos.FogEnd = 600
	atmos.FogColor = Color3.fromRGB(0,0,0)
	shrimp:Clone()
	opera:Play()
	shrimp.Parent = Player.Character:WaitForChild("HumanoidRootPart")
	shrimp.N1.CFrame = Player.Character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0,25,20)
	shrimp.N1.CFrame = shrimp.N1.CFrame * CFrame.fromEulerAnglesXYZ(1.3,0,1.55)



	local weld = Instance.new("WeldConstraint")
	weld.Parent = Player.Character:WaitForChild("HumanoidRootPart")
	weld.Part0 = Player.Character:WaitForChild("HumanoidRootPart")
	weld.Part1 = shrimp.N1

	for i, v in pairs(shrimp:GetChildren()) do
		while v.Transparency > 0 do
			v.Transparency = v.Transparency - .25
			task.wait()
		end
	end



	for i = 1, 50 do -- This is where my problem begins, this creates the fog through lighting.
		task.wait()
		atmos.FogEnd = atmos.FogEnd - 10
	end


	fire1:Clone()
	fire2:Clone()

	fire1.Parent = shrimp.Mouth
	fire2.Parent = shrimp.Mouth
	firesound:Play()

	task.wait(7)

	fire1:Remove()
	fire2:Remove()
	shrimp:Destroy()
	firesound:Stop()

	while atmos.FogEnd == 100 and atmos.FogEnd ~= 600 do
		for i = 1, 50 do
			task.wait()
			atmos.FogEnd = atmos.FogEnd + 10
		end
	end

	atmos.FogEnd = 2000
	atmos.FogColor = Color3.fromRGB(152,155,162)

	for i = 1,20 do
		task.wait(0.05)
		opera.Volume = opera.Volume - 0.05
	end

	opera:Stop()
	opera.Volume = 1

end)

Normally, the event would play out like this, eventually with the lighting returning to normal (Can’t show in clip due to length settings):

https://gyazo.com/38008ac3a7d907901f93f694cfd0e239

And this is what happens if multiple people try to do the same attack:

https://gyazo.com/c4bc15b188831f42fd6e49a9dc90d010

Help would be greatly appreciated, I tried all I could think of.

4 Likes

Have you tried checking if the fog had the desired FogEnd value ?

for i = 1, 50 do -- This is where my problem begins, this creates the fog through lighting.
	task.wait()
if atmos.FogEnd > DESIRED FOGEND then --Check if fog needs to be lowered
	atmos.FogEnd -= 10 -- "i -= f" is a shorter way of doing "i = i - f"
elseif atmos.FogEnd < DESIRED FOGEND then --If we lowered it too much, we want to clamp it to the desired FogEnd
        atmos.FogEnd = DESIRED FOGEND
end

end

Hope it helps :slight_smile:

1 Like

In the process though wouldn’t that speed up the fogs enclosing?

It depends if you have multiple copies of the same script.

Well theres a local script that activates the server event, so whenever any player presses the key, that event will fire. If that means that it will speed it up, is there a better way to do it?

Make a different script that only handles all the lighting effects based on values located in a configuration folder (optional but recommended).
For the fog :

local desiredFog =  script.configuration.DesiredFog --Your number value, can be anywhere
local atmos = game.Lighting

while task.wait() do
if atmos.FogEnd > desiredFog.Value then --Check if fog needs to be lowered
	atmos.FogEnd -= 10 -- "i -= f" is a shorter way of doing "i = i - f"
elseif atmos.FogEnd < desiredFog.Value then --If we lowered it too much, we want to clamp it to the desired FogEnd
        atmos.FogEnd = desiredFog.Value
end

end

I haven’t tested this script yet, tell me if it works :slight_smile:

Im not sure I can include the while in this… Is this meant to be here?

1 Like

Oh yes sorry how did I miss this… let me fix the original reply

1 Like

Would I need to use a separate remote event for this or could I use the same one I use to fire the script in the OP?

That’s what is supposed to happen :

  • Event fired
  • Script 1 sets desired fog value
  • Script 2 tries to make the fog reach that desired fog value

Hope it helps :slight_smile:

Im not sure if thats quite what I meant…

In the local script I activate this remote event

Could I put this in a separate script under the SAME remote event?

local desiredFog =  script.configuration.DesiredFog --Your number value, can be anywhere
local atmos = game.Lighting

while task.wait() do
if atmos.FogEnd > desiredFog.Value then --Check if fog needs to be lowered
	atmos.FogEnd -= 10 -- "i -= f" is a shorter way of doing "i = i - f"
elseif atmos.FogEnd < desiredFog.Value then --If we lowered it too much, we want to clamp it to the desired FogEnd
        atmos.FogEnd = desiredFog.Value
end

end

I feel I read somewhere you cant use the same remote event for two different things but I want to be sure.

1 Like

Normally you should be able to use the same remote event for 2 different things at once.

1 Like

Okay I will test it out and let you know if I have any problems, thank you.

1 Like

Okay so this is a lot closer to the goal im aiming for and I tweaked a little bit of your script to fit it, but the new issue is that if the fog is actively enclosing and someone else activates the remote event it will restart the fog instead of just ignoring it till it reaches desiredEndFog’s value.

This is what I tried to do to combat that but I believe the script is running to fast to where I believe it takes off (-= 10) once and then returns, or something of that sort. I’m not sure why.

This is how I have tweaked the script:

local startFog = 600
local desiredEndFog = script:WaitForChild("Configuration").End
local atmos = game.Lighting
local rs = game:GetService("ReplicatedStorage")
local se = rs:WaitForChild("ShrimpEvent")


se.OnServerEvent:Connect(function()
	while task.wait() do
		if atmos.FogEnd < startFog then return -- Here is where I believe the issue is
		elseif atmos.FogEnd > desiredEndFog.Value then
			atmos.FogEnd -= 10 -- Starts at startFog but takes off leaving it at 590, then above line returns.
		elseif atmos.FogEnd < desiredEndFog.Value then
			atmos.FogEnd = desiredEndFog.Value
		elseif atmos.FogEnd == desiredEndFog.Value then
			return
		end
	end
end)

If this doesn’t quite make sense then I could send a video.

Use BindableEvents (20 chars L)

Yes, that would help. But I think I got it. Just add a bool value

  • if changing fog == false then
  • Change fog
  • Changing fog = true
  • once fog changed, changing fog = false
  • end

or add some sort of simple queue system. Can I get a video ?

1 Like

Still, use BindableEvents, or ModuleScripts, they’d help too.

1 Like

Not too well versed on those topics yet so if I could avoid them for now that would be nice.

Nothing is wrong here with modules?