这个错误提示说明在设置模型的主要部件(PrimaryPart)之前,尝试使用了SetPrimaryPartCFrame()
方法。为了解决这个问题,你需要在调用SetPrimaryPartCFrame()
方法之前先设置模型的主要部件。
根据你的代码,可以将以下行添加到复制玩家并追踪功能的函数中,在克隆模型后立即设置主要部件:
clonedModel.PrimaryPart = clonedModel:FindFirstChild("HumanoidRootPart")
修改后的代码如下所示:
local SpawnLocation = game.Workspace.SpawnLocation -- 重生点位置
-- 虚拟形象模型
local diren = Instance.new("Model")
diren.Name = "diren"
-- 复制玩家并追踪功能
local function copyPlayer(player)
local clonedModel = player.Character:Clone()
clonedModel.Parent = game.Workspace
-- 设置主要部件
clonedModel.PrimaryPart = clonedModel:FindFirstChild("HumanoidRootPart")
local humanoidRootPart = clonedModel:WaitForChild("HumanoidRootPart")
-- 追踪玩家的位置
while true do
humanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame
wait(0.1) -- 每隔一段时间更新位置
if (humanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude < 3 then
onTouched(humanoidRootPart) -- 检查碰撞并扣除血量
copyPlayer(player) -- 复制自己作为分身,并继续追踪玩家
end
wait() -- 添加额外的延迟以避免过多更新位置
end
end
-- 碰撞检测函数
local function onTouched(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = humanoid.Health - 1
if humanoid.Health <= 0 then
-- 处理玩家血量为0的情况
part.Parent:Remove() -- 移除血量归零的角色模型(可根据实际需求进行处理)
end
end
end
game.Players.PlayerAdded:Connect(function(player)
if not player:IsDescendantOf(game.Players) then
return
end
local clonedModel = diren:Clone()
clonedModel.Parent = game.Workspace
-- 设置主要部件
clonedModel.PrimaryPart = clonedModel:FindFirstChild("HumanoidRootPart")
clonedModel:SetPrimaryPartCFrame(SpawnLocation.CFrame)
copyPlayer(player)
end)
请注意,我在两个地方添加了设置主要部件的代码。希望这次修改能够解决问题。如果还有其他问题,请随时提问。
内容由零声教学AI助手提供,问题来源于学员提问