Help With Script, Sound Playing Issues

Right now i am making a sandstorm for my Egypt Area, but when trying to play the wind sound for when the sand storm starts, the sound plays or doesn’t play and my studio just crashed when testing the untouched part.

I have 2 scripts to control the SandStorm.

First Script is a local script in StarterGui and it is the one that has problems with sound playing. (To Make This a Little Bit Easier to find out where the problem is anything that Mentions Wind, Below the first 3 wind mentions, is the part that i am confused at).

local RS = game:GetService("ReplicatedStorage")

local REs = RS:WaitForChild("REs")

local rE = REs:WaitForChild("SandStorm")

local plr = game:GetService("Players").LocalPlayer

local Storms = game.Workspace.SandStorm

local Wind = script.Sound

local WindEnder = game.Workspace.SandStorm.WindEnder

local WindStarter = game.Workspace.SandStorm.WindStarter

local SandStorm1 = Storms.SandStorm1

local SandStorm1Models1 = SandStorm1.One

local SandStorm1Models2 = SandStorm1.Two

local SandStorm1Models3 = SandStorm1.Three

local SandStorm1Models4 = SandStorm1.Four

local SandStorm1Parts1 = SandStorm1Models1.SandstormBrick1

local SandStorm1Parts2 = SandStorm1Models1.SandstormBrick2

local SandStorm1Parts3 = SandStorm1Models1.SandstormBrick3

local SandStorm1Parts4 = SandStorm1Models1.SandstormBrick4

local SandStorm1Models2Parts1 = SandStorm1Models2.SandstormBrick1

local SandStorm1Models2Parts2 = SandStorm1Models2.SandstormBrick2

local SandStorm1Models2Parts3 = SandStorm1Models2.SandstormBrick3

local SandStorm1Models2Parts4 = SandStorm1Models2.SandstormBrick4

local SandStorm1Models3Parts1 = SandStorm1Models3.SandstormBrick1

local SandStorm1Models3Parts2 = SandStorm1Models3.SandstormBrick2

local SandStorm1Models3Parts3 = SandStorm1Models3.SandstormBrick3

local SandStorm1Models3Parts4 = SandStorm1Models3.SandstormBrick4

local SandStorm1Models4Parts1 = SandStorm1Models4.SandstormBrick1

local SandStorm1Models4Parts2 = SandStorm1Models4.SandstormBrick2

local SandStorm1Models4Parts3 = SandStorm1Models4.SandstormBrick3

local SandStorm1Models4Parts4 = SandStorm1Models4.SandstormBrick4

local SandStorm1Smoke1 = SandStorm1Parts1.Sandstormbrick

local SandStorm1Smoke2 = SandStorm1Parts2.Sandstormbrick

local SandStorm1Smoke3 = SandStorm1Parts3.Sandstormbrick

local SandStorm1Smoke4 = SandStorm1Parts4.Sandstormbrick

local SandStorm1Models2Smoke1 = SandStorm1Models2Parts1.Sandstormbrick

local SandStorm1Models2Smoke2 = SandStorm1Models2Parts2.Sandstormbrick

local SandStorm1Models2Smoke3 = SandStorm1Models2Parts3.Sandstormbrick

local SandStorm1Models2Smoke4 = SandStorm1Models2Parts4.Sandstormbrick

local SandStorm1Models3Smoke1 = SandStorm1Models3Parts1.Sandstormbrick

local SandStorm1Models3Smoke2 = SandStorm1Models3Parts2.Sandstormbrick

local SandStorm1Models3Smoke3 = SandStorm1Models3Parts3.Sandstormbrick

local SandStorm1Models3Smoke4 = SandStorm1Models3Parts4.Sandstormbrick

local SandStorm1Models4Smoke1 = SandStorm1Models4Parts1.Sandstormbrick

local SandStorm1Models4Smoke2 = SandStorm1Models4Parts2.Sandstormbrick

local SandStorm1Models4Smoke3 = SandStorm1Models4Parts3.Sandstormbrick

local SandStorm1Models4Smoke4 = SandStorm1Models4Parts4.Sandstormbrick

SandStorm1Smoke1.Enabled = false

SandStorm1Smoke2.Enabled = false

SandStorm1Smoke3.Enabled = false

SandStorm1Smoke4.Enabled = false

SandStorm1Models2Smoke1.Enabled = false

SandStorm1Models2Smoke2.Enabled = false

SandStorm1Models2Smoke3.Enabled = false

SandStorm1Models2Smoke4.Enabled = false

SandStorm1Models3Smoke1.Enabled = false

SandStorm1Models3Smoke2.Enabled = false

SandStorm1Models3Smoke3.Enabled = false

SandStorm1Models3Smoke4.Enabled = false

SandStorm1Models4Smoke1.Enabled = false

SandStorm1Models4Smoke2.Enabled = false

SandStorm1Models4Smoke3.Enabled = false

SandStorm1Models4Smoke4.Enabled = false

WindStarter.Touched:Connect(function(windstart)

WindStarter.TouchEnded:Connect(function(windend)

WindEnder.Touched:Connect(function(windend2)

WindEnder.TouchEnded:Connect(function(windend3)

rE.OnClientEvent:Connect(function(mode)

if mode == "Start" then

SandStorm1Smoke1.Enabled = true

SandStorm1Smoke2.Enabled = true

SandStorm1Smoke3.Enabled = true

SandStorm1Smoke4.Enabled = true

SandStorm1Models2Smoke1.Enabled = true

SandStorm1Models2Smoke2.Enabled = true

SandStorm1Models2Smoke3.Enabled = true

SandStorm1Models2Smoke4.Enabled = true

SandStorm1Models3Smoke1.Enabled = true

SandStorm1Models3Smoke2.Enabled = true

SandStorm1Models3Smoke3.Enabled = true

SandStorm1Models3Smoke4.Enabled = true

SandStorm1Models4Smoke1.Enabled = true

SandStorm1Models4Smoke2.Enabled = true

SandStorm1Models4Smoke3.Enabled = true

SandStorm1Models4Smoke4.Enabled = true

if windstart then

if Wind.IsPaused then

Wind:Play()

Wind.Looped = true

print("In Area For Wind Started Wind")

end

end

if not windstart then

print("Not In Area For Wind")

end

if windend or windend2 or windend3 then

Wind:Pause()

print("Out Of Area For Wind Stopped Wind")

end

else

if Wind.IsPlaying then

Wind:Stop()

print("SandStorm Stopped Ending Wind")

end

SandStorm1Smoke1.Enabled = false

SandStorm1Smoke2.Enabled = false

SandStorm1Smoke3.Enabled = false

SandStorm1Smoke4.Enabled = false

SandStorm1Models2Smoke1.Enabled = false

SandStorm1Models2Smoke2.Enabled = false

SandStorm1Models2Smoke3.Enabled = false

SandStorm1Models2Smoke4.Enabled = false

SandStorm1Models3Smoke1.Enabled = false

SandStorm1Models3Smoke2.Enabled = false

SandStorm1Models3Smoke3.Enabled = false

SandStorm1Models3Smoke4.Enabled = false

SandStorm1Models4Smoke1.Enabled = false

SandStorm1Models4Smoke2.Enabled = false

SandStorm1Models4Smoke3.Enabled = false

SandStorm1Models4Smoke4.Enabled = false

end

end)

end)

end)

end)

end)

Second Script that is is ServerScriptService (The one that starts the SandStorm and end the SandStorm) (It has a wait(30) because i was testing it.)

local RS = game:GetService("ReplicatedStorage")
local REs = RS:WaitForChild("REs")
local rE = REs:WaitForChild("SandStorm")

while true do
	wait(30)

	print("Starting SandStorm")
	rE:FireAllClients("Start")
	
	
	wait(120)
	
	print("Ending SandStorm")
	rE:FireAllClients("Stop")
end

The reason the sound isn’t playing is because you’re checking if windstart exists, and from what I can tell it doesn’t appear anywhere in the script.
image

As for the entire script–for one, you’re nesting your event connections inside one another like this:

part.Touched:connect(function(hit)
    part2.Touched:connect(function(hit)
        --code goes here
    end)
end)

But this means that the connection to part2's event won’t be established until part's event has fired at least once. It also means the connection will be made several times, which may explain the crashing issue. Event connections should look like this:

part.Touched:connect(function(hit)
    --code goes here
end)
part2.Touched:connect(function(hit)
    --code goes here
end)

Finally, the method of making variables for each and every object takes up a ton of space on screen (same with the double-spacing) and it makes the code really hard to read. I’d recommend finding a way to use GetChildren() and for loops to decrease the amount of required code (it’ll also provide the benefit of not having to re-write all those lines later down the line if your game’s structure changes).

1 Like

windstart exists

so what i would have to do to make the touched events work is to put the sound events under them and put the touched events under if mode == (“Start”), but how would i make it so the touched event fires once and if your in the zone

windstart and windStarter are 2 different variable.

windstart is not defined anywhere

You should do:

local windstart = true --define it, otherwise it is nil, so the if statement is never met.

Yes, windStarter is defined, but windstart is not. Different names = different variables

local WindStarter = game.Workspace.SandStorm.WindStarter
1 Like

So for one thing, your events are strangely connected. It would look like this all tabbed out:

WindStarter.Touched:Connect(function(windstart)
  WindStarter.TouchEnded:Connect(function(windend)
    WindEnder.Touched:Connect(function(windend2)
      WindEnder.TouchEnded:Connect(function(windend3)
        rE.OnClientEvent:Connect(function(mode)
          if mode == "Start" then
            --Sandstorm.Enabled = true
            if windstart then
              if Wind.IsPaused then
                print("In area for wind started wind")
              end
            end
            if not windstart then
              print("Not in area for wind")
            end
            if windend or windend2 or windend3 then
              Wind:Pause()
              print("Out of area for wind stopped wind")
            end
          else
            if Wind.IsPlaying then
              Wind:Stop()
              print("sandstorm stopped ending wind")
            end
            --SandStorm.Enabled = false
          end
        end)
      end)
    end)
  end)
end)

This looks like the workflow of your events:

  1. Connect a function to WindStarter.Touched
  2. every time WindStarter is touched by anything, connect a function to WindStarter.TouchEnded
  3. Every time WindStarter is finished touching anything, connect a function to WindEnder.Touched
  4. Every time WindEnder is touched by anything, connect a function to WindEnder.TouchEnded
  5. Every time WindEnder is finished touching anything, connect a function to fE.OnClientEvent
  6. Finish with the rest of the function.

And that workflow is done in that specific order.

This creates a sort of butterfly effect where functions from WindStarter.Touched fire 60 frames a second, which connects a function that fires remotely less, which connects a function that fires 60 frames a second, which connects a function that fires less, which finally connects a remote event that enables/disables a ton of stuff, which is what causes your computer to crash, because by the time you have gotten all four of those events to connect:

  • you have probably been sitting in WindStarter for 30 seconds at the least and creating… 1800 event connections I believe, once per frame since .Touched fires every frame that you are actually touching the part,
  • then, .TouchEnded will fire at least 6 or 15 times depending on your character’s model, which creates another 10,800 event connections,
  • the second wave probably multiplies this total by how many frames you were sitting in WindEnder, say half a second, 30 frames. Now that is at least 324,000 event connections.
  • finally, .TouchEnded fires, which multiplies this by at least another 6 times, which makes this 1,944,000 event connections.
  • when that singular remote event fires, all of those functions will be called, and I don’t think anybody’s computer could handle a little less than 2,000,000 event connections at the same time. In fact you could multiply this by another 6 and 6 to account for all of the character’s limbs in the .Touched events, and you could multiply this by how many unanchored parts are just sitting in WindStarter or WindEnder.

If you want to just update variables on the fly using events, since it doesn’t look like you want to use them to wait for these other events to be connected and crash your computer, you could just make them change variables and end the stack right there:

local windstart = false
WindStarter.Touched:Connect(function()
    windstart = true
end)

local windend = false
WindStarter.TouchEnded:Connect(function()
    windend = true
end)

local windend2 = false
WindEnder.Touched:Connect(function()
    windend2 = true
end)

local windend3 = false
WindEnder.TouchEnded:Connect(function()
    windend3 = true
end)

rE.OnClientEvent:Connect(function(mode)
    if mode == "Start"
        --SandStorm.Enabled = true
        if windstart then
            windstart = false --you might want to do this? idk
            if Wind.IsPaused then
                Wind:Play()
                --Wind.Looped = true 
--[[I'm pretty sure you can just remove this 
if your wind is always looped]]
                print("in wind & started wind")
            end
        else
            print("not in wind")
        end
        if windend or windend2 or windend3 then
            windend = false
            windend2 = false
            windend3 = false
            --you can combine all of these into one variable to make it easy.
            Wind:Pause()
            print("out of wind paused wind")
        end
    else
        if Wind.IsPlaying then
            Wind:Stop()
            print("sandstorm stopped wind")
        end
        --SandStorm.Enabled = false
    end
end)

Another thing I would note is that you need to organize your variables so you can shorten them drastically, so probably something like this:

--at the beginning

local SS1Smoke = {}
for _,desc in pairs(game.Workspace.SandStorm.SandStorm1:GetDescendants()) do
    if desc:IsA("Smoke") and desc.Name == "Sandstormbrick" then
        SS1Smoke[#SS1Smoke + 1] = desc
    end
end

--if mode == "Start" then

for _,smoke in pairs(SS1Smoke) do
    smoke.Enabled = true
end


--else

for _,smoke in pairs(SS1Smoke) do
    smoke.Enabled = false
end

This is what I can say just about your local script, I will look at the server script in a bit, if I have the time.

2 Likes