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

Damage not being taken when touched what should i do?

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)

2 comments

1

try "hit.Parent.Humanoid:TakeDamage(115)"

0

checking the Object Browser for this stuff helps

2 answers

✓ Accepted answer

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"

4 comments

0

what did you change lol

0

and thats not it, refer to jediplocoon's answer

0

oops

0

i forgot to change it :D

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;

1 comment

0

sadly none of these solutions work :(

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