I made a script that makes the screen fade to white when you're about to enter an area. I made a debounce for it so it does not repeat over and over again. The script works perfectly fine without the if db == true then but it cancels when I do put it. I put db = true before the function.

db = true
function onTouch(hit)
    if db == true then -- the line that breaks script
	    db = false
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) 
		player.PlayerGui.FadeGui.Frame.BackgroundTransparency = 0.9 
	    wait(0.01)
	    player.PlayerGui.FadeGui.Frame.BackgroundTransparency = 0.7
	    wait(0.01)
	    sound:Play()
	    player.PlayerGui.FadeGui.Frame.BackgroundTransparency = 0.5
	    wait(0.01)
 -- the part above repeats 10 times more but with smaller numbers for the decreasing transparency
	    db = true
	    if player.leaderstats.Health.Value == true then 
		    script.Parent.Health.Health.Visible = true -- just something else for GUI
		    end
		    end 
		    end
workspace.BeginTeleport.Touched:Connect(onTouch)

4 comments

0

Try making "db" a local variable.

0

You may have to check if whatever touched it is a player. The part may already have been touched by surrounding parts and will run immediately. You can do this through using :GetPlayerFromCharacter() on the Players service.

0

use wait(1) right before ;-;

0

to check if its actually a player use if player then ... end