Hello. The old post about this issue was deleted because I posted a wrong script.
So, I have an OwnerOnlyDoor which should kill you (if it’s red) if you try to enter it and you’re not the owner.
Whenever I try to enter a door which is owned by somebody else, it sets my Health to 0, however, it does not kill me, and console pops up such errors:
Humanoid is not a valid member of Hat, script Line 10
Humanoid is not a valid member of Accessory, script Line 10
The script:
local Owner = script.Parent.Parent.Parent.Parent:WaitForChild("Owner")
script.Parent.Touched:connect(function(hit)
if hit.Parent.Name == Owner.Value.Name then --Assuming that this is an object value
print("youre da boss")
else
print("check check your neck")
if script.Parent.BrickColor == BrickColor.new("Really red") then
print("you shall not pass")
hit.Parent.Humanoid.MaxHealth = 0
elseif script.Parent.BrickColor == BrickColor.new("Lime green") then
print("my boss let you enter")
else
print("boss im confused")
end
end
end)
Maybe changing MaxHealth to Health would fix this issue? I’m not sure, so any help would be appreciated.
Thanks.
I believe the problem is still the same as your last post.
There is a part in accessories called Handle, and since that part can activated Touched functions, it will. The parent of this part, the Accessory, does not have a child named “Humanoid” so when you say…
hit.Parent.Humanoid.MaxHealth = 0
…It will throw an error saying that “Humanoid” is not a valid member of accessory.
Try adding this to the beginning of your function:
if not hit.Parent:FindFirstChild("Humanoid") then return end
This should cancel the function if hit is not a sibling of “Humanoid”.
Also, I would probably set Health to 0 instead of MaxHealth.
local Owner = {"Owner Name"}
for i,AdminName in ipairs(Owner) do
if AdminName == Owner then
script.Parent.Touched:connect(function(hit)
if hit.Parent.Name == AdminName then
print("boss")
else
if script.Parent.BrickColor == BrickColor.new("Really red") then
print("you shall not pass")
hit.Parent.Humanoid.MaxHealth = 0
elseif script.Parent.BrickColor == BrickColor.new("Lime green") then
print("my boss let you enter")
else
print("boss im confused")
end
end
end
end
end
end)
This will probably work!,Make sure this is a script inside part
This is unnecessarily adding on. You don’t need a loop or a table here, assuming OP’s tycoon system is single-owner-based. It introduces unneeded expense and still lacks validation of a player character or their contents on all levels. It’s also connecting a touch event each iteration…?
I don’t recommend following this. OP’s code is already fine, just that they need to check if a character has a humanoid before acting.
local Owner = script.Parent.Parent.Parent.Parent:WaitForChild("Owner")
script.Parent.Touched:connect(function(hit)
local Hum = hit.Parent:FindFirstChildOfClass("Humanoid")
if not Hum then return end
if hit.Parent.Name == Owner.Value.Name then --Assuming that this is an object value
print("youre da boss")
else
print("check check your neck")
if script.Parent.BrickColor == BrickColor.new("Really red") then
print("you shall not pass")
Hum.MaxHealth = 0
elseif script.Parent.BrickColor == BrickColor.new("Lime green") then
print("my boss let you enter")
else
print("boss im confused")
end
end
end)
This is a common problem most people encounter when first using the Touched event. A solution for this is relatively direct, the problem here is that you are touching this part with your Hat or Accessory and since you have it checking for humanoid in the most widely used method (local Humanoid = hit.Parent:FindFirstChild("Humanoid") or in your case you are checking by verifying the Hits parent name value which in the case of it being a Accessory or Hat would be a part inside the Accessory instance so when that if statement is ran it is checking if the Accessory’s name is the same as the Owner value, which will be false. A way to fix your problem is by (Also I suggest not doing any of this in workspace as you are currently)
local Owner = script.Parent.Parent.Parent.Parent:WaitForChild("Owner")
script.Parent.Touched:connect(function(hit)
local Humanoid = Hit.Parent:FindFirstChild("Humanoid") or Hit.Parent.Parent:FindFirstChild("Humanoid")
if Humanoid.Parent.Name == Owner.Value.Name then
print("youre da boss")
else
print("check check your neck")
if script.Parent.BrickColor == BrickColor.new("Really red") then
print("you shall not pass")
Humanoid.Health = 0
elseif script.Parent.BrickColor == BrickColor.new("Lime green") then
print("my boss let you enter")
else
print("boss im confused")
end
end
end)
Again by no means is this the best method to do what you are attempting to do but it should get the job done. Your problem is
As you are indexing something that could potentially not be there.