Touched part problem please help

script:

game.Workspace.Part.Touched:Connect(function(onhit)
	local player = game.Players.LocalPlayer

	local character = player.Character  or player.CharacterAdded:Wait()
	
local 	humanoid = character:FindFirstChildOfClass("Humanoid")

	if  humanoid then

		print("working")



	end
 


end)

i put this on a local script

1 Like

What is the problem? I have already tried it in roblox studio and it identifies the humanoid

2 Likes

try it not in a localscript, just a normal script.

1 Like

when i touch the part it aint doin nothin

Workspace.Part.Script:9: attempt to index nil with ‘Character’
with a normal script btw

Just do: make a part, put a script inside of it, delete the print(“Hello World”)
and type

local part = script.Parent
part.Touched:Connect(function()
local h = part.Parent:FindFirstChild("Humanoid")
if h then
print("working")


i’m typing in devforum, so I don’t really know how many ends you need

1 Like

k thx also its one end and one end)
t h i r t y char

Alright. Have a good day/night

2 Likes
game.Workspace.Part.Touched:Connect(function(onhit)
	local character = onhit.Parent

	
        local humanoid = character:FindFirstChildOfClass("Humanoid")
	if  humanoid then
		print("working")
	end
end)

Your code is client sided

1 Like

i finished this long time lol
this one giving me trouble tho

script

local isTouched = false
game.Workspace.Part2.Touched:Connect(function(hit)
	if(game.Players:GetPlayerFromCharacter(hit.Parent) or game.Players:GetPlayerFromCharacter(hit.Parent.Parent))  then
		if isTouched == true then
			return
end
		 if isTouched == false then
		
			isTouched = true
			print("hello")
			
			task.wait(3)
		isTouched = false
			
	end 	 	
	end
end)

You don’t use return on events

1 Like

wdym events explain please

t h i r t y c h a r
do you mean use local script since normal script is server side?
(i think)

Delete the return, your code should work

1 Like

ohhhh it worked tysm
t h i r t y c h a r

This is kind of messy so here’s a cleaner version

local isTouched = false
workspace.Part2.Touched:Connect(function(hit)
	if isTouched then return end
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		isTouched = true
		task.delay(3, function() isTouched = false end)
		print("A character with the humanoid", humanoid, "touched me!")
	end
end)
1 Like

what the heck is task.delay(3, function()
and FindFirstChildWhichIsA?

plz explain

well i understand the if isTouched then return end

task.delay(duration, callback): calls the callback function after duration seconds
Instance:FindFirstChildWhichIsA(class): Finds the first child who’s :IsA(class) returns true

task.delay(3, function() isTouched = false end)
In this case, task.delay is used to set the isTouched to false after 3 seconds.

hit.Parent:FindFirstChildWhichIsA("Humanoid")
:FindFirstChildWhichIsA("Humanoid") is used to find the first child which is a Humanoid within the character of the part touched

Task Library: task
:FindFirstChildWhichIsA: Instance:FindFirstChildWhichIsA

1 Like

nice explaination now i understand alot better
thank you

1 Like

FindFirstChildOfClass() would be slightly more performant due to its implementation. In this instance “Humanoid” is a class-type and a base class. Event callbacks execute in their own thread of execution so the task.spawn() is unnecessary, task.wait() would be the better alternative as it doesn’t create an additional thread but merely yields the current thread for some specified length of time. Finally, the script can be rewritten to accommodate for a player’s character’s accessories/equipped tools.

local part = workspace.Part2

local debounce = false

part.Touched:Connect(function(hit)
	if debounce then return end
	
	local hitModel = hit:FindFirstAncestorOfClass("Model") --detects tools/accessories
	if hitModel then
		local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
		if hitHuman then
			debounce = true
			--do code here
			task.wait(3)
			debounce = false
		end
	end
end)
2 Likes