Part touch effecting system (gui)

hello, i need help with this script that I have made that is not working right. Here’s the script.

local player = game.Players.LocalPlayer
local playerG = player.PlayerGui

local cold = game.Workspace.effection.cold

local radiation = playerG["radiation system"].Frame.Frame
local volume = 1000 

local humanoid = player.Character:WaitForChild("Humanoid")

local effect = false

volume = math.clamp(volume, 0, 1000)

-- setting cold system
task.wait(1.02)
cold.Touched:Connect(function()
	effect = true
	while volume > 0  and effect do
		script.Parent.Parent.Parent.Enabled = true -- the gui will appear 
		volume = volume - 1 
		script.Parent.BackgroundColor3 = Color3.new(0.521569, 0.690196, 0.709804)
        -- resize for the volume
		script.Parent:TweenSize(UDim2.new(volume/126, 0, 0.7, 0), 'Out', 'Linear', 0)
		wait()
		if volume == 0 then -- will repeat until the player is not touching the part
			repeat
				humanoid.Health = humanoid.Health -1
				task.wait(2.55)
			until
			game.Workspace.Baseplate.Touched:Connect(function(off) 
				if off.Parent:WaitForChild("Humanoid") then
					effect = false
					print("stop frezze")
					while volume > 0 and not effect do 
					volume = volume +1 
                    -- resize back to the full volume 
					script.Parent:TweenSize(UDim2.new(volume/126, 0, 0.7, 0), 'Out', 'Linear', 0)
					wait()
					if volume ~= 1000 then -- close the gui, when reach amount to full volume
						task.wait()
						print("enabled gui")
		                script.Parent.Parent.Parent.Enabled = false -- close gui
		               end
					end
				end
			end)
		end
	end
end)

the issue is when you touch the part/cold the gui is not in the right size place correctly and if the player touch baseplate or untouched the part the gui will flicker a lot and stop and when move while touching the baseplate the script signal repeat a lot of time until you stop.if you can help me with this problem,
I gradually appreciate i

1 Like

For the wrong GUI size, try replacing all UI element’s scale to use scale instead of offset.

ok, then I will tried that for the gui placement

Try adding a debounce to solve that issue.

Debounce as in:

local debounce = false
local part = -- whatever the part is

part.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        -- do whatever
        wait(2) -- wait to prevent the touched event from firing again 
        debounce = false
    end
end)
cold.Touched:Connect(function()
	if debounce == false then
		debounce = true
		if volume > 0 and debounce then
			playerG["radiation system"].Enabled = true
			volume = volume -1
			script.Parent.BackgroundColor3 = Color3.new(0.521569, 0.690196, 0.709804)
			-- resize for the volume
			script.Parent:TweenSize(UDim2.new(volume/126, 0, 0.7, 0), 'Out', 'Linear', 0)
			wait()
			if volume <= 0 then
				humanoid.Health = humanoid.Health -1
			end
			wait(2) -- wait to prevent the touched event from firing again 
		debounce = false
			if volume < 1000 and not debounce then
				volume = volume +1
				script.Parent:TweenSize(UDim2.new(volume/126, 0, 0.7, 0), 'Out', 'Linear', 0)
				wait()
				playerG["radiation system"].Enabled = false
			end
		end
		
	end
end)

the script isn’t working at all.

Here:

local debounce = false
cold.Touched:Connect(function()
	if debounce == false then
		debounce = true
		if volume > 0 then
			playerG["radiation system"].Enabled = true
			volume = volume -1
			script.Parent.BackgroundColor3 = Color3.new(0.521569, 0.690196, 0.709804)
			-- resize for the volume
			script.Parent:TweenSize(UDim2.new(volume/126, 0, 0.7, 0), 'Out', 'Linear', 0)
			wait()
			if volume <= 0 then
				humanoid.Health = humanoid.Health -1
			end
			if volume < 1000 and not debounce then
				volume = volume +1
				script.Parent:TweenSize(UDim2.new(volume/126, 0, 0.7, 0), 'Out', 'Linear', 0)
				wait()
				playerG["radiation system"].Enabled = false
			end
		end
		wait(2) -- wait to prevent the touched event from firing again 
		debounce = false
	end
end)

Try that or put the wait(2) and debounce = false in a section where you think finishes of this function.