ZBLOG

15:24:03.071 Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this. - 服务器 - Script:48

这个错误提示说明在设置模型的主要部件(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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=2789

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?