How to tell if player is touch a wall or not?

ye how would i do the title, thanks :slight_smile:

Please research before you make a post. You would need a script in your wall and you would utilize the .Touched event

Here is what you would have in your script:

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

Part.Touched:Connect(function(Hit) --Fires when something hits the part
    local Plr = Players:GetPlayerFromCharacter(Hit.Parent) or Players:GetPlayerFromCharacter(Hit.Parent.Parent) --Gets the Player that hit the part (if it was a player)
    if Plr then --Checks if it was a player
        --Here goes what you want to happen
    end
end)

i have a lot of parts in my game, so it wouldn’t really be practical to have every wall get a scrip like that, is there a way to detect it from the player?

In that case, I would make a invisible part for each wall to be some kind of hitbox, and then use the .Touched event for those.

for _, v in pairs(workspace.Folder:GetChildren()) do
v.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player then
-- code
end
end)
end

Like @BobbieTrooper said, you can use a pairs loop to loop through all the walls and create a touched event for each one.

-- local script

local RunService = game:GetService('RunService')

local Plr = game:GetService('Players').LocalPlayer

function isTouchingWall()
	local Char = Plr.Character
	if Char == nil then return end

	local HRP:BasePart? = Char:FindFirstChild('HumanoidRootPart')
	if HRP == nil then return end

	local touching = false

	local part = Instance.new('Part')
	part.Anchored = true

	for i = -180, 180, 2 do
		part.Rotation = Vector3.new(0,i,0)
		
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = { Char }
		
		local result = workspace:Raycast(HRP.Position,part.CFrame.LookVector * 1.5,params)
		
		if result and result.Instance.Parent and not game.Players:GetPlayerFromCharacter(result.Instance.Parent) then
			touching = true
			break
		end
	end

	part:Destroy()

	return touching
end

RunService.RenderStepped:Connect(function()
	print(isTouchingWall())
end)

Have a folder named “Walls” in workspace. Put your walls there.

--Script in StarterCharacterScripts
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")

humanoid.Touched:Connect(function(hit, limb)
if hit.Parent == workspace.Walls then
print("plr touched wall")
--Optionally fire a RemoteEvent here...
end
end)

You would want to do something like this since it would be ideal for performance and would most likely outperform all other methods listed here.