Check if a player is still touching a part

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Detect if a player is still touching a part after 1 second

  2. What is the issue? Include screenshots / videos if possible!
    I want it to damage the player, wait 1 second, then if the player is still touching the part to damage them again, and this continues until the player is dead or moves. However, if the player stands still on the part, they don’t take any damage until they move.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    None, I’m pretty stuck and don’t really have any ideas

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is the code:
*context: first, a grid is made with different layers of parts. the bottom layer (lava) is what I want to damage the player. The script looks to see if any of the parts in the entire grid have a “dps” value, and if it is not equal to 0. If it isn’t then it waits for the player to step on the part, then damages them.

local plrs = game:GetService("Players")
local ws = game:GetService("Workspace")

local plr = plrs.LocalPlayer

local debounce = false

repeat task.wait(0.1) until game:IsLoaded()

for _, part in pairs(ws.biomes.plains:WaitForChild("grid"):GetDescendants()) do
	if part:FindFirstChild("dps") ~= nil then
		if part.dps.Value ~= 0 then
			part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") ~= nil then
					if debounce == false then
						debounce = true
						
						hit.Parent.Humanoid.Health -= part.dps.Value
						
						task.wait(1)
						
						debounce = false
					end
				end
			end)
		end
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Should be able to get away with a small local function

function IsPlayerTouchingPart(Part, Player)
	if Player.Character then -- Player can't touch stuff if Character doesn't exist.
		for _, TouchingPart in pairs(Part:GetTouchingParts()) do
			if TouchingPart:IsDescendantOf(Player.Character) then
				return true
			end
		end
	end
end

In your case, I’d replace , Player) with , Character) then change anything in the func that says Player.Character with just Character

4 Likes

You can use .TouchEnded to determine whenever character leaves the part

1 Like