[SOLVED] How can I detect when a part starts to collide and ends locally?

Thank you to everyone who helped me earlier.

I’m developing a placement system, similar to that of SCP-3008, but I’ve run into a problem.

How can I detect if a block is collided with something and when it stops doing so from the local side?

I just want to know if “SelectionPart” is colliding with something, and also if it stops doing so.

Here’s my local script:

-- /// MAIN LOCALS ///


local LocalPlayer = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local PlrCursor = LocalPlayer:GetMouse()
local UserInputService = game:GetService("UserInputService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StartPlacingEvent = ReplicatedStorage:WaitForChild("Events").StartPlacingEvent
local PlacedEvent = ReplicatedStorage:WaitForChild("Events").Placed

local Orientation = 0
local Distance = 10

local PlayerIsSelecting = false
local NowSelectingName = nil

-- //KEYBINDS//

local PlaceKey = Enum.KeyCode.E
local RotateSidesKey = Enum.KeyCode.R
local RotateUpDownKey = Enum.KeyCode.T

-- /// MAIN LOCAL FUNCTIONS ///

-- Position Updater
local function updateFurniturePosition(furniture)
	local HeadPart = LocalPlayer.Character:WaitForChild("Head"):WaitForChild("ModelPart").fpart
	local direction = HeadPart.CFrame.LookVector
	local newPosition = HeadPart.Position + direction * Distance
	
	furniture.Position = newPosition
end

local function checkCollision(part)
	print("Touch grass")
end


-- Tools Functions
local function place()
	PlacedEvent:FireServer(NowSelectingName)
	print("Place")
end

local function rotateside()
	print("RotateSide")
end

local function rotateupdown()
	print("RotateUp/Down")
end
------------------------------

-- /// SCRIPT ///

StartPlacingEvent.OnClientEvent:Connect(function(Model)
	if PlayerIsSelecting == false then
		PlayerIsSelecting = true
		
		print("Selected Model: "..Model.Name)
		
		NowSelectingName = Model.Name
		
		local SelectionPart = Model:WaitForChild("SelectorPart")
		
		if PlayerIsSelecting then
			
		end
		
		for _, part in ipairs(Model:GetDescendants()) do -- Modify model property on Local
			if part:IsA("BasePart") then
				part.CanCollide = false
				part.Transparency = 0.5
				part.CastShadow = false
			end
		end

		repeat
			task.wait()
			for _, part in ipairs(Model:GetDescendants()) do -- Set the Model positions.
				if part:IsA("BasePart") then
					updateFurniturePosition(part)
				end
			end
		until PlayerIsSelecting == false
		
	else
		PlacedEvent:FireServer(NowSelectingName)
		PlayerIsSelecting = false
		print("What model was selected: "..NowSelectingName)
		wait(0.05)
		NowSelectingName = nil
	end
end)

-- Functions/tools for building
UserInputService.InputBegan:Connect(function(input)
	if PlayerIsSelecting == true then
		-- Place Function
		if input.KeyCode == PlaceKey then
			place()
		end

		-- Rotate at sides Function
		if input.KeyCode == RotateSidesKey then
			rotateside()
		end

		-- Rotate up and down sides Function
		if input.KeyCode == RotateUpDownKey then
			rotateupdown()
		end
	end
end)

Thanks for any help. :slightly_smiling_face:

Try this function, and put the LocalScript in StarterCharacterScripts:

local Part = game.Workspace.Part -- set Part to the part you want it to detect

Part.Touched:Connect(function(hit)
-- you can use "hit" for the part that it is touching if needed
end

Sorry I was at my sleeping time, I tryed something like that but, I want to know too if I can use a diferent function or smth like that…

If you don’t want to use the Part.Touched event, try Part:GetTouchingParts() instead. This will return a table of every part that is physically touching that part that also has CanCollide enabled. If you need to get a specific part touching it, try using an for i, v loop. Some example code here:

local Part = game.Workspace.Part -- set Part to the part you want it to detect

for i, v in pairs(Part:GetTouchingParts()) do
    if v then
        -- this means it IS touching other parts
    elseif not v then
        -- this means it is NOT touching other parts
    end
end

Just sorry for not answer, just I follow ur reply and I tried to find a solution by a forum who explains it better, GetTouchingParts() without cancollide trick, that basically works on client side, and is more clean and fast than Touched() function, what I was trying not to use.

Ty for the help. :slight_smile:

You’re linking to a post that couldn’t possibly be more clearly marked as obsolete. I recommend taking another read and updating your link/post!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.