how come the commented one works but the for loop doesn’t work?
local highlight = game.ReplicatedStorage.Visuals:WaitForChild("Highlight")
--script.Parent.Touched:Connect(function(hit)
-- local player = game.Players:GetPlayerFromCharacter(hit.Parent)
-- if player then
-- print(player.Name)
-- local newhigh = highlight:Clone()
-- newhigh.Parent = player.PlayerGui
-- newhigh.Adornee = script.Parent.Parent
-- script.Parent.TouchEnded:Connect(function()
-- newhigh:Destroy()
-- end)
-- end
--end)
for _, v in script.Parent:GetChildren() do
if v:IsA("BasePart") and v.Name == "HitBox" then
v.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
print(player.Name)
local newhigh = highlight:Clone()
newhigh.Parent = player.PlayerGui
newhigh.Adornee = script.Parent.Parent
script.Parent.TouchEnded:Connect(function()
newhigh:Destroy()
end)
end
end)
end
end
for _, v in script.Parent:GetChildren() do
if v:IsA("Part") and v.Name == "HitBox" then
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
print(player.Name)
local newhigh = highlight:Clone()
newhigh.Parent = player.PlayerGui
newhigh.Name = "highlight"
newhigh.Adornee = script.Parent.Parent
end
end)
end
end
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local high = player.PlayerGui:FindFirstChild("highlight")
high:Destroy()
end
end)
Try this. You are getting an array of Models with :GetChildren(), not BaseParts.
What you should do is look for the hitbox inside the model.
for _, v in script.Parent:GetChildren() do
if v:FindFirstChild("Hitbox") then
v.Hitbox.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
print(player.Name)
local newhigh = highlight:Clone()
newhigh.Parent = player.PlayerGui
newhigh.Adornee = script.Parent.Parent
local connection
connection = script.Parent.TouchEnded:Connect(otherPart)
if game.Players:GetPlayerFromCharacter(otherPart) == player then
newhigh:Destroy()
connection:Disconnect()
end
end)
end
end)
end
end