Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I get Humanoid outside function(hit) ?

Guys, I want to get Humanoid with function(hit) but i want to use it outside of the function(hit). How do I do that?

Here is My Script

local Knife = script.Parent
local db = true

if db == true then
	Knife.Touched:Connect(function(hit)
		local Humanoid = hit.Parent:WaitForChild("Humanoid")	
	end)

	Humanoid:TakeDamage(1)
end

1 answer

Try this.

local Knife = script.Parent
local db = true
local Humanoid

if db == true then
    Knife.Touched:Connect(function(hit)
        Humanoid = hit.Parent:WaitForChild("Humanoid")    
    end)

    Humanoid:TakeDamage(1)
end

2 comments

0

The .Touched event doesn't yield, so when it tries to damage the humanoid, it would always be nil. You'd either want to loop until the humanoid or wait a bit then check if humanoid exists.

0

Pretty sure this is to inside a sword script, but checking if a humanoid exists might be a smart idea to be honest

This discussion is preserved as it was. New answers, comments, and votes are closed.