Roblox:尝试使用“背包”索引 nil

问题描述 投票:0回答:1

我正在尝试在 Roblox 上为镐制作一个脚本,为玩家提供矿石,但我收到错误并且没有任何反应。该代码位于玩家所持有的工具内的本地脚本中。下面是我的代码:

local tool = script.Parent
local canMine = false

local PS = game:GetService("Players")

local function onTouch(otherPart, player, name)
    local oreTier = otherPart:WaitForChild("OreTier").Value
    local gold = otherPart:FindFirstChild("Gold").Value
    local oreNaame = otherPart:WaitForChild("OreToSpawn").Value
    local toolTier = tool.ToolTier.Value
    if not oreTier then return end

    local oreToSpawn = game.ReplicatedStorage.Items.Resources.Ores:FindFirstChild(oreNaame) 
    
    if oreTier <= toolTier and canMine then
        
        local oreExists = oreToSpawn
        
        print("The ore was hit.")
        
        if oreExists then
            local ore = oreToSpawn:Clone()
            ore.Parent = player.Backpack
            otherPart:Destroy()
        end


        end
    end
--end

local function slash()
    local str = Instance.new("StringValue")
    str.Name = "toolanim"
    str.Value = "Slash" 
    str.Parent = tool
    canMine = true
    tool.Handle.SwordSlash:Play()
end

tool.Activated:Connect(slash)
tool.Pick.Touched:Connect(onTouch)
lua roblox
1个回答
0
投票

https://create.roblox.com/docs/reference/engine/classes/BasePart#Touched

Touched
仅提供碰撞部分。
player
name
未使用,因此为零。您需要通过例如检索播放器工具父级。

© www.soinside.com 2019 - 2024. All rights reserved.