How to detect if character touched something

Hello!

Im trying to make script that located in StarterGui and its a Local script, this script should check if players character (or specific part of body) touched something and gets name of what he touched.

The question is how can i detect if character touched something?

3 Likes

Based from what you said I think what you’re looking for is something like this. This article is from the Roblox Developer Hub which has some other useful sources as well. The funtion itself goes as follows.

part.Touched:Connect(function(hit) -- hit in the parameters being what the part touched
     -- if you want to get the players character you do as follows
    if hit.Parent:FindFirstChild("Humanoid") then -- the players character has a humanoid so by getting the parent of hit and if hit's parent has a humanoid then it's the player. You can replace Humanoid with something that can be found in the player's character as well
        print(hit.Parent.Name.." is a player") -- put what you want here
    end
end)

Hope this helps.

1 Like

i mean i want something like this

local player = game.Players.LocalPlayer
local deb = false
local camera = game.Workspace.CurrentCamera
local character = player.Character

character:FindFirstChild("HumanoidRootPart").Touched:Connect(function(part)
	if deb == false and part.Name == "tp" then
		deb = true
		local torso = character:FindFirstChild("Torso")
		if part.Parent.Name == "Level1" then -- Change here to add more
			local levelC = game.ReplicatedStorage.MAPS.Level2:Clone() -- and here!
			local tpTo = levelC:FindFirstChild("Speed") --and maybe here (optional)
			levelC.Parent = camera
			wait(0.3)
			torso.CFrame = tpTo.CFrame
			player:FindFirstChild("leaderstats").Stage.Value = player:FindFirstChild("leaderstats").Stage.Value == 2
			wait(0.3)
			deb = false
		end
	end
end)

but when im joining im getting this error
Screenshot_134

Change your Character variable’s value to Player.Character or Player.CharacterAdded:Wait()

You should also retrieve the HumanoidRootPart from their Humanoid property: RootPart.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Humanoid.RootPart
7 Likes

Yey no errors now!!! ITS WORKS!!!

Thank you very much!

2 Likes