lua - 尝试使用“CameraMaxZoomDistance”索引 nil 我尝试搜索解决方案,但没有找到一个
问题描述
我试图让玩家的最大变焦距离更多地取决于他们拥有的力量(力量),因为力量越大,角色越大。
但我收到上述错误:
尝试使用“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
解决方案
你的错误是说那game:GetService("Players").LocalPlayer
是零。根据LocalPlayer的文档:
此属性仅针对 LocalScripts(以及它们所需的 ModuleScripts)定义,因为它们在客户端上运行。对于服务器(Script 对象在其上运行其代码),此属性为零。
您正在尝试访问特定角色模型的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
推荐阅读
- python - 如果我删除我的 css,在我的关于 django 模板的页面中面对这个问题,那么它工作得很好,否则每当我用来显示图像时它都会给我一个错误
- if-statement - Pine 脚本交易,无法使用 If else 语句
- python-3.x - 将整数列表转换为逗号分隔的整数
- python - 获取与目标变量相关的特征列表
- python - 如何使用 pyOpenCL 在 GPU 内存上分配内存?
- java - Java中有没有办法直接在charArray中输入?
- c# - ASP.NET CORE DI - 从 EF Core 的 Database.GetDbConnection() 中解析 IDbConnection
- wildfly - 使用启动脚本通过 CLI 将 Zipkin 跟踪添加到 WildFly
- php - 从 MySQL 表中删除一行
- hyperledger-fabric - 链码 bor 已在通道 mychannel 上成功定义并重试:'bor' 的链码定义存在,但未安装链码