Cloning a tool into the player's backpack?

I have a zombie survival mini-game and I’m going to change the way the weapons work by making them only available during the mini-game,
I have a BoolValue called GameInProgress in Replicated Storage
and I am trying to make a script where when
game.ReplicatedStorage.Values.GameInProgress = true” it will clone the tool into the players backpack, and if “game.ReplicatedStorage.Values.GameInProgress = false” , it destroys the tool.
Could somebody help me figure something out please? I’d greatly appreciate it.

2 Likes

You’d have to use a remote event if you’re trying to get it into just a local player’s backpack.
Also don’t forget boolvalues have a .Value to them, so you’d reference it as game.ReplicatedStorage.Values.GameInProgress.Value = false

Local script

local SomeEvent = game.ReplicatedStorage.SomeEvent

if game.ReplicatedStorage.Values.GameInProgress.Value then  -- condition
    SomeEvent:FireServer()
end

Server script

--this listens for the event

local SomeEvent = game.ReplicatedStorage.SomeEvent
local tool = game.ReplicatedStorage.SomeTool --- Reference your tool here. You can put it in either Replicated or ServerStorage because this is a server script. I'd recommend keeping it in ServerStorage, because it's more secure.

SomeEvent.OnServerEvent:Connect(function(player)
    toolclone = tool:Clone()
    toolclone.Parent = player.Backpack
end
3 Likes

if GameInProgress == true then
tool:Clone()
tool.Parent = game.Players.LocalPlayer.Backpack
end

Just an idea of what to do.

1 Like

Perfect example right here. Use this.

1 Like

Oh wait I didn’t read this right. This is what you’d do.
You want the tools in everybody’s backpack, so you’d just need a server script lol

Obviously this is just the base of how you’d do it, you need to adjust this to your own use.

Script

local tool = game.ReplicatedStorage.SomeTool
local GameInProgress = game.ReplicatedStorage.Values.GameInProgress

GameInProgress:GetPropertyChangedSignal("Value"):Connect(function()  -- whenever the value changes
    if GameInProgress.Value then
        for i, v in pairs(game.Players:GetChildren()) do
            local toolclone = tool:Clone()
            toolclone.Parent = v.Backpack
        end
    else
        for _, v in pairs(game.Players.GetChildren()) do  
            for i, item in pairs(v.Backpack:GetChildren()) do
                if item:IsA("Tool") then
                    item:Destroy()  -- clears every tool in the backpack
                end
            end
        end
    end
end)
2 Likes

Its actually a easy thing but here it is:

local theTool = game.ReplicatedStorage.Tool
local GameInProgress = game.ReplicatedStorage.Values.GameInProgress
GameInProgress:GetPropertyChangedSignal("Value"):Connect(function()
 --Fires a event everytime GameInProgress value is changed

 if GameInProgress.Value == true then
--Checks if game is in progress

       for _,v in pairs(game.Players:GetPlayers()) do
       theTool:Clone().Parent = v.Backpack

       -- Go through every player and clone the tool stored so it can be given to all players
       end
 elseif GameInProgress.Value == false then
       --Checks if the game is not in progress
  
       for _,v in pairs(game.Players:GetPlayers()) do
            local insidePlayerTool = v.Backpack:FindFirstChild(theTool.Name)
            --Goes through every player and try finding the tool.

            if insidePlayerTool then
                 --If it finds then it destroys

                 insidePlayerTool:Destroy()
            end
       end
 end

end)
2 Likes

Thank you so much! This script works and solves the issue i’ve been facing. Once again thanks:+1:

1 Like

The value can always either be true or false if it’s a BoolValue object so you can just use an else block there, also you can do if value then end to verify a value is truthy instead of comparing it like that to make code look cleaner and shorter with the same functionality (other than the fact that nil and false will be treated equally which shouldn’t be a problem here).

2 Likes

Actually I started doing these when I got some problems in using debounce because I used it like “if debounce then”, after I started using “if debounce == true then” I could use it way cleaner and better.

The moments I do like how you said is to compare a thing that can be nil.

Logically there are some cases I use like how you said, in the debounces case I prefer using the way I said.

(Its just my opinion but yes it can maybe be cleaner to others)

1 Like

Thank you for posting this - I couldn’t get a tool to work properly after an NPC placed it in the local player’s back pack. Switching to an event processing as you have described, fixed the issue instantly.