Needing help with a script

Hi,

This might be a bit difficult to explain but I am making a script that works when you have 0 or under “Vision Time” by covering your screen with a Frame, and when it is over 0, the Frame turns Invisible. I used GetPropertyChangedSignal but it keeps playing the sound when it is over 0 Vision Time. Does anyone have an idea how to make it only play once after it is over 0?

local player = script.Parent.Parent.Parent.Parent
local leaderstats = player:WaitForChild("leaderstats"):WaitForChild("Vision Time")
local frame = script.Parent
local sound = script.Parent.Parent.SwitchSound

local function updateScreen()
	if leaderstats then
		local value = leaderstats.Value
		
		if value <= 0 then
			frame.Visible = true
			sound:Play()
		else
			frame.Visible = true
			sound:Play()
		end
	end
end

leaderstats:GetPropertyChangedSignal("Value"):Connect(updateScreen)
1 Like

because in the else statement you also play the sound so no matter what the value is the sound is gonna play

You’re not changing anything when it’s over 0.

local player = script.Parent.Parent.Parent.Parent
local leaderstats = player:WaitForChild("leaderstats"):WaitForChild("Vision Time")
local frame = script.Parent
local sound = script.Parent.Parent.SwitchSound

local function updateScreen()
	if leaderstats then
		local value = leaderstats.Value
		
		if value <= 0 then
			frame.Visible = true
			sound:Play()
		else
			frame.Visible = false
			sound:Stop()
		end
	end
end

leaderstats:GetPropertyChangedSignal("Value"):Connect(updateScreen)

I managed to fix it by using a variable.

local player = script.Parent.Parent.Parent.Parent
local leaderstats = player:WaitForChild("leaderstats"):WaitForChild("Vision Time")
local frame = script.Parent.Parent
local sound = script.Parent.Parent.SwitchSound

local toggled = false

local function updateScreen()
	if leaderstats then
		local value = leaderstats.Value
		
		if value <= 0 then
			frame.Enabled = true
			sound:Play()
			toggled = true
		else
			if toggled == true then
				frame.Enabled = true
				sound:Play()
				toggled = false
			end
		end
	end
end

leaderstats:GetPropertyChangedSignal("Value"):Connect(updateScreen)

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