Part already changes color when still touching

Im currently learning lua and wanted to make a test script, as long as ur touching it the part should be pink and when u leave it it becomes green. But for some reason it already becomes green while still touching it and kinda spasms out.

local part = script.Parent
local pink = Color3.fromRGB(253, 20, 242)
local green = Color3.fromRGB(48, 207, 0)
local db1 = false
local db2 = false

part.touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db1 == false then
			db1 = true
			part.Color = pink
			wait(0.5)
			db1	= false
			end
	end
end)


part.TouchEnded:Connect(function(nohit)
	if nohit.Parent:FindFirstChild("Humanoid") then
		if db2 == false then
			db2 = true
		
			part.Color = green
			wait(0.5)
			db2 = false
			end
	end
end)

Is the part anchored?

If not, anchor it.

If that doesn’t work, it most likely is touching the ground or something.

Try moving it in the air or make a line of code that stops the event when it detects that it’s touching the ground – Something like this:

if hit.Name == "Baseplate" then
     return false
end

1 Like

but doesnt it already specify to only run the script if it hits the humanoid? i might be wrong im new to Lua

Oh yeah, I didn’t see that part. Send me a vid of it.

1 Like

Here you go.

https://gyazo.com/1d72aa63aa6ad07b34101398c55edb5e

Hm. I think I see the problem. When you stop moving it fires the “TouchEnded” event, therefore it changes color. But when you move again, it fires the “Touched” event. The touched event in my opinion isn’t very accurate. You may have to use a debounce although it still might not solve the problem.

If you want, you can use a different kind of event such as a proximity prompt.

1 Like

I dont really need a game mechanic to change the parts colors, this was just to test what i recently learned about lua, thanks for your help tho! :slight_smile:

1 Like

Yeah, it isn’t your fault that the code isn’t working. It’s just the touch event isn’t accurate. My first game used a touch event and sometimes it would be hard to actually trigger it.

1 Like

Hello, so i tried to do a proximity test that the parts color changes to green when holding the proximity and becomes pink when done, im not familiar with proximity, whats wrong here?

local part = script.Parent
local pink = Color3.fromRGB(253, 20, 242)
local green = Color3.fromRGB(48, 207, 0)
local color = false


local function onPromptTriggered()
	
		part.color = pink
	
end


local function onPrompHoldBegan()
	part.color = green
end

You can do:

local part = script.Parent
local proximityPrompt = part.ProximityPrompt
local pink = Color3.fromRGB(253, 20, 242)
local green = Color3.fromRGB(48, 207, 0)
local color = false

local function onPromptTriggered()
	part.Color = pink
end

local function onPromptHoldBegan()
	part.Color = green
end

proximityPrompt.PromptButtonHoldBegan:Connect(onPromptHoldBegan)
proximityPrompt.Triggered:Connect(onPromptTriggered)
1 Like

You just forgot to connect the events. Adding onto what has previously been suggested your original scripts were fine, “.Touched” and “.TouchEnded” are just very inconsistent events which will regularly fire even when it seems like they shouldn’t.

I’ve decided to rewrite your original script with the additions/fixes I’d implement. This is working relatively well.

local players = game:GetService("Players")
local part = script.Parent

local lastTouched = 0

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			lastTouched = tick()
			part.Color = Color3.new(1, 0, 1)
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			task.delay(3, function()
				if tick() - lastTouched > 1 then
					part.Color = Color3.new(0, 1, 0)
				end
			end)
		end
	end
end)

Here’s the model file for reproduction purposes.

repro.rbxm (3.0 KB)

2 Likes

Hey, i connected the proximity functions but it still doesnt work.

local part = script.Parent
local pink = Color3.fromRGB(253, 20, 242)
local green = Color3.fromRGB(48, 207, 0)
local color = false
local proximityPrompt = part.ProximityPrompt

local function onPromptTriggered()
	
	part.Color = pink
	
end

local function onPromptHoldBegan()
	
	part.Color = green
	
end


proximityPrompt.PromptButtonHoldBegan:Connect(onPromptHoldBegan)
proximityPrompt.Triggered:Connect(onPromptTriggered)

also i tried the code u send me and changed the color codes to rgb because its easier for me to use and it doesn turn green when u get off?

local players = game:GetService("Players")
local part = script.Parent

local lastTouched = 0

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			lastTouched = tick()
			part.Color = Color3.fromRGB(255, 0, 251)
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			task.delay(3, function()
				if tick() - lastTouched > 1 then
					part.Color = Color3.fromRGB(12, 255, 0)
				end
			end)
		end
	end
end)

https://gyazo.com/ac8cdb9c3b6293120eb5f8aaff135e05

I already provided a reproduction file.

1 Like

Yea but what about the proximity, i know i found an alternative but the reason i made this script was to learn more about lua since im learning it, so i better also learn about proximity prompts while im at it.

local prompt = script.Parent
local part = prompt.Parent

local function onPromptTriggered()
	part.Color = Color3.new(1, 0, 1)
end

local function onPromptHoldBegan()
	part.Color = Color3.new(0, 1, 0)
end

prompt.PromptButtonHoldBegan:Connect(onPromptHoldBegan)
prompt.Triggered:Connect(onPromptTriggered)

repro.rbxm (3.6 KB)

1 Like

why did mine not work and urs did?

You probably had a wrong reference somewhere.

1 Like