Function disconnects when player resets/dies?

Hi there, I have a club floor that changes color of the music’s loudness. The issue is, is that the function disconnects after the player has resetted or died. I do not have an attempt as I am unsure. What I’m trying to achieve, is that after a player dies or resets, the function that visualizes the club floor, reconnects.

SOLUTION
Moved the script to starterscripts instead.

(Focus on the renderstepped function and not the last one as that is for something else).

local sound = workspace.Sound
local plr = game.Players.LocalPlayer
local parts = workspace:WaitForChild("parts")
local floor = parts:WaitForChild("Club Floor")
local pillars = workspace.pillar

local pillar1 = pillars.Model1.neon.Part
local pillar2 = pillars.Model2.neon.Part
local pillar3 = pillars.Model3.neon.Part
local pillar4 = pillars.Model4.neon.Part

local button = plr.PlayerGui:WaitForChild("Settings"):WaitForChild("GUI"):WaitForChild("panels"):WaitForChild("ScrollingFrame"):WaitForChild("Visualizers"):WaitForChild("Club Floor"):WaitForChild("button")
local on = true

local ColorSilent = Color3.fromRGB(0, 0, 0)
local ColorLoud = Color3.fromRGB(10, 162, 243)

local rs = game:GetService("RunService")

local lastSoundLevel = sound.PlaybackLoudness

button.Text = ("ON")
button.BackgroundColor3 = Color3.fromRGB(90,90,90)

rs.RenderStepped:Connect(function()
	if on then
		local difference = (sound.PlaybackLoudness / 2.2) - lastSoundLevel

		difference /= 100

		difference = math.clamp(difference,0.62,1)
		
		local change = ColorSilent:Lerp(ColorLoud,difference)
		
		floor.Color = change
		
		pillar1.Color = change
		pillar2.Color = change
		pillar3.Color = change
		pillar4.Color = change
	end
end)

button.MouseButton1Click:Connect(function()
	workspace.clickeffect:Play()
	if on == true then
		on = false
		button.Text = ("OFF")
		button.BackgroundColor3 = Color3.fromRGB(50,50,50)
	else
		on = true
		button.Text = ("ON")
		button.BackgroundColor3 = Color3.fromRGB(90,90,90)
		
		floor.Color = Color3.fromRGB(10, 162, 243)
		pillar1.Color = Color3.fromRGB(10, 162, 243)
		pillar2.Color = Color3.fromRGB(10, 162, 243)
		pillar3.Color = Color3.fromRGB(10, 162, 243)
		pillar4.Color = Color3.fromRGB(10, 162, 243)
	end
end)

The video below shows this happening.

Where is your script located? I think your code should be fine.

I’m not sure if we can help you with current info, but you can for sure narrow stuff down if it doesn’t error by printing after every define and seeing if it makes it up to the RunService loop point.

Under StarterGUI

Is it in the folder directly? Or under a GUI? In either case, try moving it into StarterPlayer > StarterPlayerScripts.

It’s in a folder.

Could you send me a screenshot of it in the Explorer window please? Could you also check if the ‘Settings’ GUI in the StarterGui has ResetOnSpawn enabled?

Alright.

image

What do you mean ‘Settings’? The settings gui is for toggling different aspects but does have a club floor visualizer option in it. I don’t think that GUI is related since by default, it’s on.

Gotcha. Just checking since you have a line of code in your script that calls WaitForChild on it, so I was wondering if it was maybe in an infinite yield. Do you have any of those ‘infinite yield possible’ warnings in your output after resetting? (it may take some time for those warnings to show up)

What is infinite yield? Nevermind just checked there is an option for reset on spawn, it’s ticked.

Uh, I just tested again and for some reason now the function isn’t even connecting despite I didn’t change anything. Also, I did find a infinite yield: Infinite yield possible on 'Workspace:WaitForChild("Humanoid")'

And now it is connected when I re-test, what the hell is going on

An infinite yield is like a wait() in your script that goes on for an infinite amount of time.

This might be because of all the WaitForChild functions you have in your script, the game might render the objects you’re waiting for differently every time you playtest. Not sure though.

I believe that’s another script, shouldn’t be related to your visualizer.

So it isn’t infinite yield neither?

I don’t think so, I’m stumped. Do you want to invite me into a Team Create so I can take a look?

Yeah sure. PM me.

If you need a local script to execute each time the player’s character is reloaded/respawned then move the script to the StarterCharacterScripts folder, it’ll be parented to each created character model of the player and simultaneously execute when this automatic parenting occurs.