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;
2 comments
try "hit.Parent.Humanoid:TakeDamage(115)"
checking the Object Browser for this stuff helps