[英]Attempt to index nil
我正在嘗試創建一個取件,但是當您取件時會彈出一個錯誤。 這是錯誤:
Workspace.LogPickup.LogPickupScript:8: attempt to index nil with 'Parent'
腳本的第 8 行是變量 player。
這是代碼:
local log = script.Parent
local logGuard = false
local function onTouch(partTouched)
local character = partTouched.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local playerStats = player:FindFirstChild("leaderstats")
local playerLogCount = playerStats:FindFirstChild("Has Log")
if humanoid and logGuard == false then
log.Transparency = 1
log.CanCollide = false
logGuard = true
playerLogCount.Value = 1
wait(5)
log.Transparency = 0
log.CanCollide = true
logGuard = false
end
end
log.Touched:Connect(onTouch)
此錯誤告訴您在第 8 行中使用鍵 'Parent' 索引了一個 nil 值。
不允許索引 nil 值。
在第 8 行中搜索以下任何內容:
.Parent
[Parent]
:Parent
並找到:
humanoid.Parent
現在你知道 humanoid 是一個你可能不索引的 nil 值。
要么確保 character:FindFirstChildWhichIsA("Humanoid") 始終返回預期值,要么檢查它是否確實為您編制索引。
local character = partTouched.Parent
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
local player = humanoid and game.Players:GetPlayerFromCharacter(humanoid.Parent)
local playerStats = player and player:FindFirstChild("leaderstats")
local playerLogCount = playerStats and playerStats:FindFirstChild("Has Log")
短路是避免索引錯誤的一種簡單方法,只要您可以確定您得到 nil 或預期值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.