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

Damage not being taken when touched what should i do?

Asked by 3 years ago

I have been having this problem when the objects is touched the object does not take damage so i have a sword and a zombie and when the sword touches the zombie the zombie should take damage but it doesn't what should i do?

script.Parent.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild("Humanoid")
then
    hit.Parent.Humanoid.TakeDamage = 115
end
end)
1
try "hit.Parent.Humanoid:TakeDamage(115)" jediplocoon 877 — 3y
0
checking the Object Browser for this stuff helps AAzterr 27 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

your syntax is incorrect use:

script.Parent.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild("Humanoid")
then
    hit.Parent.Humanoid:TakeDamage(115)
end
end)

if the answer helped you, don't forget to mark it as "answered"

0
what did you change lol matiss112233 258 — 3y
0
and thats not it, refer to jediplocoon's answer matiss112233 258 — 3y
0
oops Newh0hrizonCz 58 — 3y
0
i forgot to change it :D Newh0hrizonCz 58 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

The first thing you should do when interacting with touched characters is ensuring that what's touching the part is actually a player (so the logs aren't flooded with errors c;). I made a handy function that will get the player from a descendant of a character, and will return nil if the player wasn't found.

local Players = game:GetService("Players")

function getplayer(instance)
    for _, v in pairs(Players:GetPlayers()) do
        if v.Character and (instance:IsDescendantOf(v.Character) or instance == v.Character) then
            return v
        end
    end
end

Using this function, we can now easily determine whether or not a touching part is a Character or not and damage accordingly with a simple if statement checking whether or not player will be nil (see below)

script.Parent.Touched:Connect(function(hit)
    local player = getplayer(hit)
    if player then
        player.Character.Humanoid:TakeDamage(115)
    end
end)

leading our final code to be

local Players = game:GetService("Players")

function getplayer(instance)
    for _, v in pairs(Players:GetPlayers()) do
        if v.Character and (instance:IsDescendantOf(v.Character) or instance == v.Character) then
            return v
        end
    end
end

script.Parent.Touched:Connect(function(hit)
    local player = getplayer(hit)
    if player then
        player.Character.Humanoid:TakeDamage(115)
    end
end)

If this answer was helpful, please mark as solution, thx c;

0
sadly none of these solutions work :( Etrnal_Dev 9 — 3y

Answer this question