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)
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"
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;