【问题标题】:attempt to index nil with 'CameraMaxZoomDistance' I tried searching for solutions, but didn't find even one尝试使用“CameraMaxZoomDistance”索引 nil 我尝试搜索解决方案,但没有找到一个
【发布时间】:2021-08-29 11:48:53
【问题描述】:

我试图让玩家的最大变焦距离更多地取决于他们拥有的力量(力量),因为力量越大,角色越大。

但我收到了上述错误:

尝试使用“CameraMaxZoomDistance”索引 nil

这是我的代码:

            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

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    您的错误是说game:GetService("Players").LocalPlayer 为零。根据LocalPlayer 的文档:

    此属性仅为 LocalScripts(以及它们所需的 ModuleScripts)定义,因为它们在客户端上运行。对于服务器(Script 对象在其上运行其代码),此属性为 nil。

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

    if powr.Value > 1000 then
        -- get the character model
        local character = hum.Parent
    
        -- lookup the player based on the character
        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
    
        -- adjust the player's camera zoom distance
        player.CameraMaxZoomDistance = powr.Value / 50
    end
    

    【讨论】:

      猜你喜欢
      • 2016-05-09
      • 1970-01-01
      • 2021-09-04
      • 2022-01-05
      • 2021-08-02
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      相关资源
      最近更新 更多