尝试使用'CameraMaxZoomDistance'索引空值。我尝试搜索解决方案,但连一个都没找到

我正在尝试根据玩家的力量使最大缩放距离更长,因为角色越大力量越大。

但是我得到了上面的错误:

尝试使用'CameraMaxZoomDistance'索引空值

这是我的代码:

            hum:WaitForChild("BodyDepthScale").Value = .5 + (powr.Value / 250)
            hum:WaitForChild("BodyHeightScale").Value = .5 + (powr.Value / 250)
            hum:WaitForChild("BodyWidthScale").Value = .5 + (powr.Value / 250)
            hum:WaitForChild("HeadScale").Value = .5 + (powr.Value / 250)
            if powr.Value > 1000 then
                game:GetService("Players").LocalPlayer.CameraMaxZoomDistance = powr.Value / 50
            end
            if powr.Value > 200 then
                print('higher')
                hum.MaxHealth = powr.Value / 2
            end
点赞
用户2860267
用户2860267

你的错误是说 game:GetService("Players").LocalPlayer 是空的。根据 LocalPlayer 的文档:

此属性仅为 LocalScripts(以及被它们引用的 ModuleScripts)定义,因为它们在客户端运行。对于服务器(在那里 Script 对象运行它们的代码),该属性为 nil

你正在尝试访问特定角色模型的 Player 对象,有几种不同的方法可以获取它。你已经可以访问 humanoid 对象,它位于角色模型本身中,所以我建议使用 Players:GetPlayerFromCharacter 函数来定位 Player 对象。

如果 powr.Value > 1000 then
    -- 获取角色模型
    local character = hum.Parent

    -- 基于角色查找玩家
    local PlayerService = game:GetService("Players")
    local player = PlayerService:GetPlayerFromCharacter(character)
    if not player then
        warn("Could not locate player from character : ", character.Name)
        return
    end

    -- 调整玩家的相机缩放距离
    player.CameraMaxZoomDistance = powr.Value / 50
end
2021-06-13 16:03:34