Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Find if a player leaves a part?

I'm trying to make a script where color correction changes when you enter a certain part, but whenever the player touches the part the color correction keeps going and the screen goes black. I don't know when and when not to use debounce, so if I used it incorrectly then that's why. Script:

p = game.Players.LocalPlayer
c = p.Character
camera = workspace.CurrentCamera

local debounce = false

repeat wait()
until c
if debounce == false then
	debounce = true
workspace.PinesBiome.Touched:Connect(function()
	p.PlayerGui.ScreenGui.Biome.Text = "Pines"
	p.PlayerGui.ScreenGui.Biome.Dropshadow.Text = "Pines"
	for i = 1,50 do
		camera.correct.Brightness = camera.correct.Brightness - .03
		camera.correct.TintColor = Color3.new(125/255,143/255,123/255)
	end
 end)
end
workspace.PinesBiome.TouchEnded:Connect(function()
	debounce = false
end)

4 comments

0

use local variables & try not to use "repeat wait() until". "local c = p:WaitForChild("Character")" works just fine

0

Debounces are wrong too

0

U cant just say the script broke u have to rewrite ur debounces lol?

0

^^^ I did, and I tried the old script and it still doesn't work.

2 answers

✓ Accepted answer

Local scripts tend to load before the character does. To get by this you'll need to wait for any instances you're defining.

You should also properly acquire services when using any using the GetService() method.

To properly wait for the character (Instead of relying on a repeat loop), use CharacterAdded:Wait(). This Wait() function is called until the character loads which will then return it to us.

local Players = game:GetService("Players")

local p = Players.LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()

local partTouch = workspace:WaitForChild("PinesBiome")

Notice how we define a variable that waits for the "PinesBiome" part? We need to make sure the part loads in so we can bind our events.


Debounces serve as on/off switches to make sure that events are not constantly firing.

####Currently your code looks like this:

if debounce == false then
	debounce = true
	workspace.PinesBiome.Touched:Connect(function()
	
	end)

--missing end for if statement

You have no end to close the if-statement but that's an easy problem to fix. The main issue I want to address is that you're checking the debounce BEFORE setting up your Touched events. This will run only once and never check for debounce again!

To fix this, place an if-statement into each Touched/TouchedEnded event with their own debounce. That way neither event interferes and no event is being spammed.

local debounce1 = true
local debounce2 = true

partTouch.Touched:Connect(function()
	if debounce1 then
		debounce1 = false
		
		--code
		
		debounce1 = true
	end
end)

partTouch.TouchEnded:Connect(function()
	if debounce2 then
		debounce2 = false
		
		--code
		
		debounce2 = true
	end
end)

Finally we need another switch so that both events know that the player is inside. This bool variable serves to check that if the player is inside then run code which reverts that ColorCorrection you are using. I had simply named it inside.


You also edit the ColorCorrection properties in a weird way. When you use a numerical for-loop, you should take advantage of the variable which shows the iteration it's on. Since you want to achieve a darkening effect you would do it on the ColorCorrection.Brightness property.

--Use fromRGB instead of the method you were using!
camera.correct.TintColor = Color3.fromRGB(125,143,123)
for i = 0, -.1, -.01 do
	camera.correct.Brightness = i
	wait()
end

The Brightness property for the ColorCorrection object works in a way where anything below 0 makes the lighting darker. So we need a loop that counts down. Toy with the values I had given above to get a proper look.

In our TouchEnded event we could revert all this back to normal by making sure that the player was inside with our inside bool variable.

--Inside of TouchEnded:

		if inside then
			inside = false
			camera.correct.TintColor = Color3.fromRGB(255,255,255)
			camera.correct.Brightness = 0
		end

####Your final code should look like:

local p = game.Players.LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()
local camera = workspace.CurrentCamera

local debounce1 = true
local debounce2 = true

local inside = false

local partTouch = workspace:WaitForChild("PinesBiome")

partTouch.Touched:Connect(function()
	if debounce1 then
		debounce1 = false

		if inside == false then
			inside = true
			p.PlayerGui.ScreenGui.Biome.Text = "Pines"
			p.PlayerGui.ScreenGui.Biome.Dropshadow.Text = "Pines"
	
			camera.correct.TintColor = Color3.fromRGB(125,143,123)
			for i = 0, -.1, -.01 do
				camera.correct.Brightness = i
				wait()
			end
		end

		debounce1 = true
	end
end)

partTouch.TouchEnded:Connect(function()
	if debounce2 then
		debounce2 = false
		
		if inside then
			inside = false
			camera.correct.TintColor = Color3.fromRGB(255,255,255)
			camera.correct.Brightness = 0
		end

		debounce2 = true
	end
end)

#####If I missed something then reply back with a comment or if there are any concerns with my explanations.

1 comment

0

Thanks, there's a bug with first person but i'll see if I can fix it.

Your debounces are wrong try this...

	p = game.Players.LocalPlayer
	c = p.Character
	camera = workspace.CurrentCamera
	 
	local debounce = false
	 
	repeat wait()
	until c
	
	   
	workspace.PinesBiome.Touched:Connect(function()
	if debounce == false then
 debounce = true
	    p.PlayerGui.ScreenGui.Biome.Text = "Pines"
	    p.PlayerGui.ScreenGui.Biome.Dropshadow.Text = "Pines"
	    for i = 1,50 do
	        camera.correct.Brightness = camera.correct.Brightness - .03
	        camera.correct.TintColor = Color3.new(125/255,143/255,123/255)
	    end
	 end)
	end
	workspace.PinesBiome.TouchEnded:Connect(function()
	    debounce = false
	end)


Move the if inside the function and then turn it true

1 comment

0

That made the script break.

This discussion is preserved as it was. New answers, comments, and votes are closed.