【问题标题】:Keycard door not working when i put the keycard at replicates storage当我将钥匙卡放在复制存储时,钥匙卡门不工作
【发布时间】:2022-01-20 04:13:00
【问题描述】:

我有一个钥匙卡门,但是当我把它放在replicatesStorage 中时它不起作用(它是一个游戏通行证钥匙)有人可以帮助我它只在放入starterPack 时才起作用它目前在StartGui 上使用localScript 提供* * 这里是代码:


script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == "Clearance1" then
        script.Parent.CanCollide = false
        script.Parent.Transparency = 0.5
        wait(0.5)
        script.Parent.CanCollide = true
        script.Parent.Transparency = 0
    end
end)

【问题讨论】:

标签: lua roblox


【解决方案1】:

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)

每次触摸部件时,它都会检查它是否是玩家。如果是,它会检查玩家是否拥有该物品。如果是这样,它会运行代码。

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2019-10-25
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多