LUA(Roblox) - 将工具克隆到玩家背包中所需的帮助

发布于 2025-01-26 09:43:45 字数 860 浏览 1 评论 0原文

因此,我正在用商店制作游戏,而我遇到的问题是,我有一个复制工具,因此当玩家购买该工具时,我可以将其克隆到玩家背包中。问题在于,无论我将其消失的工具放到哪里。我尝试了ServerStorage,ReplicatedStorage,Workspace和脚本内部,但是每次测试游戏时,它都会从任何地方删除它。我会在下面放我的脚本,但我不知道它是否已损坏,我无法对其进行测试,因为该工具可以从我放置的任何地方删除。这是我在Startergui的TextButton中的LocalsScript中的代码。

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    local tool = script.Tool:Clone()
    tool.Parent = player.Backpack
    print("player recieved tool")
end)

这是从工作空间中的脚本中的一个接触事件触发的。

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        print(hit.Parent)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        game.ReplicatedStorage.Shop:Fire(player)
    end
end)

因此,这些可能有效,但我只需要在实际工具消失方面提供帮助即可。任何帮助! (如果您想要图像或视频,我可以记录下来并发送链接)

so I am making a game with a shop and the problem I am having is that I have a tool in ReplicatedStorage so I can clone it into the players backpack when the player buys that tool. The problem is that wherever I put the tool it disappears. I have tried ServerStorage, ReplicatedStorage, Workspace and inside the script but everytime I test the game it removes it from anywhere I put it. I will put my script below but I don't know if that is broken yet, I haven't been able to test it because the tool gets removed from anywhere I put it. Here is my code in a LocalScript in a TextButton in StarterGUI.

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    local tool = script.Tool:Clone()
    tool.Parent = player.Backpack
    print("player recieved tool")
end)

and that gets triggered from a Touched event in script in a part in Workspace.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        print(hit.Parent)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        game.ReplicatedStorage.Shop:Fire(player)
    end
end)

So, Those might work but I only need help with the actual tool vanishing. Any help appreciated! (If you want images or videos I can record them and send a link)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

棒棒糖 2025-02-02 09:43:45

似乎问题可能是服务器不知道您获得了该物品,因为它全部在Localside上完成。尝试使用像remoteevent之类的东西。

Localside:

local player = game.Players.LocalPlayer
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

script.Parent.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer()
end)

您需要为ReplicatedStorage添加remoteevent。
服务器端:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local ToolPrefab = --add the directory of the weapon here
RemoteEvent.OnServerEvent:Connect(function(plr)
    local ToolClone = ToolPrefab:Clone()
    ToolClone.Parent = plr.Back
end)

It seems as the problem might be that the server doesn't know you got the item since it's all done on the localside. Try using something like a RemoteEvent.

Localside:

local player = game.Players.LocalPlayer
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

script.Parent.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer()
end)

You'll need to add a RemoteEvent to ReplicatedStorage.
Server side:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local ToolPrefab = --add the directory of the weapon here
RemoteEvent.OnServerEvent:Connect(function(plr)
    local ToolClone = ToolPrefab:Clone()
    ToolClone.Parent = plr.Back
end)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文