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)
game.Workspace.Part.Touched:Connect(function(onhit)
local character = onhit.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
print("working")
end
end)
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)
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)
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
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)