StarterPack 在某种程度上已经被复制了。
如果您使用 ReplicatedStorage,则必须将其添加到 PlayerInstance.Backpack。
另外,script.Parent.Touched 仅适用于服务器端。
如果你想访问玩家的背包,你可以使用game.Players.PLAYERNAME.Backpack
你这样做的方式实际上不是一个好主意。 (没有冒犯的意思)
我建议将物品留在 StarterPack 中。如果您真的不想这样做,您可以通过将其放入背包中以编程方式进行操作。像这样:
-- Server script
game.Players.PlayerAdded:Connect(function(player) -- Runs when a player joins the game
player.CharacterAdded:Connect(function() -- Runs when that player respawns or their character loads
local itemCopy = game.ReplicatedStorage.Clearance1:Clone() -- Creates a copy of the item
itemCopy.Parent = player.Backpack -- Puts the item in the player's backpack (inventory)
end)
end)
该代码的作用是:每次玩家生成时,它都会克隆该物品并将其放入他们的库存中。
现在要检查用户在触摸门时是否有物品,您可以执行以下操作:
-- Server script
script.Parent.Touched:Connect(function(part) -- Activates when a part touches the doorf
local player = game.Players:GetPlayerFromCharacter(part.Parent) -- Gets a player from the part that touched
if player and player.Backpack:FindFirstChild("Clearance1") then -- Makes sure there is a player, and the player has the keycard in their inventory
script.Parent.CanCollide = false -- Makes the part uncollidable
script.Parent.Transparency = 0.5 -- Sets the part to have transparency
wait(0.5) -- Waits half a second
script.Parent.CanCollide = true -- Makes the part collidable
script.Parent.Transparency = 0 -- Makes the part not transparent
end
end)
每次触摸部件时,它都会检查它是否是玩家。如果是,它会检查玩家是否拥有该物品。如果是这样,它会运行代码。