当尝试将物品克隆到背包时收到无效响应

我正在尝试从ReplicatedStorage中克隆一个对象到玩家的背包中,当一个对象构成被触摸时,我的代码看起来很好,但它一直从'clone.parent = player.backpack'收到无效响应

local replicatedtorage = game:GetService("ReplicatedStorage")
local Sword = replicatedtorage:FindFirstChild("Sword")

local part =  game.Workspace.Part

local player = game.Players.LocalPlayer
local clone = Sword:Clone()

part.Touched:Connect(function(hit)
    local humanoid = hit.parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
    clone.Parent = player.Backpack
end
end)
点赞
用户13661384
用户13661384

这看起来像是一个服务器脚本,不能像客户端那样访问 Players.LocalPlayer,因为服务器没有本地玩家。通过 Players:GetPlayerFromCharacter() 可以获取触碰该部件的 Player,该函数需要传递一个实例,并将返回其角色为该实例的 Playernil

part.Touched:Connect(function(hit)
    local character = hit.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    if player then
        clone.Parent = player.Backpack
    end
end)

这应该可以立即在你的脚本中使用,并可以替换你现有的 Touched 连接。

2020-10-05 21:50:37