IsDescendantOf() and FindFirstAncestor() both don't work for me?

To keep things short, I am creating a wave survival game.

I have a castle built and enemies which attack the castle and if a player is close enough, the player too. In my script which controls the damage done (if the enemy does not have a weapon) I state that if the object hit by the enemy is a descendant of the castle and not a player then the castle is to take damage (castle’s health is an attribute and starts at 10,000);


local sp = script.Parent

local children = sp:GetChildren()

local parts = {}

local debounce = false

local humanoid = sp.Humanoid

for i,v in pairs(children) do
	if v.Name == "Left Arm" or v.Name == "Right Arm" or v.Name == "Left Leg" or v.Name == "Right Leg" then
		v.Touched:Connect(function(hit)
			
			local players = game:GetService("Players"):GetChildren()
			
			local characters = {}
			
			for i,v in pairs(players) do
				
				game.Workspace:WaitForChild(v.Name)
				
				if debounce == false and sp:GetAttribute("weapon") == false then
					if hit.Parent.Name == v.Character.Name and humanoid.Health > 0 then

						debounce = true
						hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health-sp:GetAttribute("damage")
						if sp:GetAttribute("element") then
							if sp:GetAttribute("element") == "Fire" then
								hit.Parent:SetAttribute("fire", math.random(2,4))
							elseif sp:GetAttribute("element") == "Frost" then
								hit.Parent:SetAttribute("frost", math.random(1,4)) 
							elseif sp:GetAttribute("element") == "Poison" then
								hit.Parent:SetAttribute("poison", math.random(3,8))
							elseif sp:GetAttribute("element") == "Lightning" then
								hit.Parent.Humanoid.Sit = true
							end
						end
						wait(0.75)
						debounce = false

					elseif hit:IsDescendantOf(workspace.Castle) then
																		
						if workspace.Castle:GetAttribute("health") > 0 then
							
							debounce = true
							
							local health = workspace.Castle:GetAttribute("health")
							
							workspace.Castle:SetAttribute("health", health-sp:GetAttribute("damage"))
							
							wait(0.75)
							debounce = false
						end
					end
				end
			end
		end)
	end
end

I have also tried changing out IsDescendantOf() with hit:FindFirstAncestor(“Castle”), neither of which seem to render that the hit object is a descendant of the castle. I have all the parts of the castle stored in a model so parts which are hit are UNDOUBTABLY descendants of the castle. I have also tested this by making the script print hit.Parent.Parent and hit.Parent.Parent.Parent (the third descendant being the most distant descendant from the castle) and sure enough, prints “Castle”. The player damage part works fine as well.

There are no errors returned by this script.

If you print out the name of Hit what does it give? Possibly the thing being touched is not part of it?

“hit” is for sure a descendant of “Castle”.

Oh god wait, I just realised that I am testing my game in “run” mode. So the script can’t find any players, therefore run. My bad!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.