Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get Humanoid outside function(hit) ?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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
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. the8bitdude11 358 — 3y
0
Pretty sure this is to inside a sword script, but checking if a humanoid exists might be a smart idea to be honest Blxefirx 31 — 3y
Ad

Answer this question