I am trying to make a pet that follows you and when opponents enter the area around the pet, they take continuous damage. (like area burn damage)
To do this I have the pet being cloned into the player’s torso. Then inside the pet is a part that is a large sphere that is non-collide and 0.8 transparent. Inside the part (named cloud) is a script that is supposed to trigger when a player or NPC part touches the cloud. It checks if it is the owner of the pet and if not, it should do damage.
I have everything working except the damage component. Here is what I have tried so far for the script in the cloud part:
function onTouched(hit)
local plr = script.Parent.Parent.Parent.Parent
if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent ~= plr then
hit.Parent.Humanoid.Health.Value = hit.Parent.Humanoid.Health.Value - 1
end
end
and
function onTouched(hit)
local plr = script.Parent.Parent.Parent.Parent
while hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent ~= plr do
hit.Parent.Humanoid.Health.Value = hit.Parent.Humanoid.Health.Value - 1
wait(1)
end
end
Well, why don’t you use magnitude instead of having a sphere part? Just check if a player is within bounds of a pet by using magnitude.
local cloud = script.Parent.Cloud
local ownername = ""
while true do
for i, v in pairs(game.Players:GetPlayers()) do
if v.Name == ownername then return end
local hrp = v.Character:FindFirstChild('HumanoidRootPart')
if hrp then
local dist = (cloud.Position - hrp.Position).Magnitude
if dist < 8 then -- in studs
-- do damage here
end
end
end)
wait(.1)
end
However, if you still want to do it your method, which I don’t really recommend, you’re doing the touched function completely wrong.
You shouldn’t use that many .Parent’s because what if it hits an accessory? That’s going to have a different directory than a body part.
Here is how it should look:
local debounce = false
function onTouched(hit)
local character = hit.Parent:FindFirstAncestorWhichIsA('Model')
if character and not debounce then
local humanoid = character.Humanoid
humanoid .Health.Value = humanoid.Health.Value - 1
debounce = true
wait(1)
debounce = false
end
end
In general you should add print statements around the parts you suspect it isn’t working.
Did you properly connect OnTouched to the Touched event?
Touched event will only trigger once, when something moves and touches it. This means to recreate the burn affect, you would have to either keep track of if a specific humanoid is still touching the object using touchended, or make a loop and comparing the distance between humanoids and the script.
Thank you.
I will try the magnitude method.
I may still keep the sphere for a visual aid so players can see the range as I plan on increasing the magnitude as the pet levels up, but change the script to your first example.
Thank you for the advice. I always forget to test with print statements.
Then I start to freak out when there are no errors and I am stuck staring at the screen for hours…
I used the 2nd solution first and that worked with the physical sphere, but then when testing, the sphere also blocked projectiles in my gun kit.
I tried the magnitude way from your first example and had to tweak it a little to where it doesn’t dmg the owner of the pet, but I can’t figure out how to also get it to damage NPCs.
It is not game breaking or anything if it can’t, but it would be more intuitive.
If it helps my NPCs all spawn in:
So what I would do in this situation would just have a table with all the possible people who will be damaged. So I would get the players and the NPC’s and then use unpack() to unpack the table so the contents of NPC and player instances are combined in one table to be looped through.
local cloud = script.Parent.Cloud
local ownername = ""
while true do
local targetList = {unpack(game.Players:GetPlayers()), unpack(game.Workspace.Basemap.Bots:GetChildren())}
for i, v in pairs(targetlist) do
if v.Name == ownername then return end
local hrp = v.Character:FindFirstChild('HumanoidRootPart')
if hrp then
local dist = (cloud.Position - hrp.Position).Magnitude
if dist < 8 then -- in studs
-- do damage here
end
end
end)
wait(.1)
end
Please let me know if you need any more assistance. I’m always glad to help.
Players who have a force field are immune to taking damage from Humanoid:TakeDamage().
If a player has a force field and you want them to take damage, you subtract their health value, and if you don’t want that to happen use TakeDamage().