Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i remove the gear from player during the game?

Asked by
Glvt102 11
3 years ago

Well, basically, the player has a gun, but later in the game the gun should be removed and replaced with another gun

2 answers

Log in to vote
0
Answered by 3 years ago

player.Backpack:FindFirstChild("ToolName"):Destroy() instead of using Destroy() use Maid. Destroy isn't good, but it works.

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

If you want to actually keep the gun I recommend a folder in ServerStorage. This will be a script in ServerScriptService since you can do this on the server and you need to be on the server to access ServerStorage. I'll write it out for you.

local rep = game:GetService("ReplicatedStorage") --gets replicatedstorage
local event = Instance.new("BindableEvent") --creates bindable event, fire this event when you want replace gun
event.Name = "Gun" --sets the event's name, can change it to whatever you want
local storage = game:GetService("ServerStorage") --gets serverstorage
local folder = Instance.new("Folder") --creates new folder
folder.Name = "Guns" --sets folder name change it to whatever you want
local newgun = folder:FindFirstChild("Gun") --can change name, can change path to where the gun that you give is

game.Players.PlayerAdded:Connect(function(plr) --listens for a player joined, plr is the player who joined
    local Backpack = plr.Backpack --gets player's backpack
    local tool = Backpack:WaitForChild("Gun"):Clone() --can replace name, clone so we can manage its parent
    tool.Parent = Backpack --sets clone parent to backpack in case it wasn't
    tool.Name = "GunTemp" --sets the name to a different one for the next step
    Backpack:WaitForChild("Gun"):Destroy() --destroys the old tool
    event.Event:Wait() --waits for event to occur
    tool.Parent = storage --once event fired, saves the first gun
    newgun.Parent = Backpack --sets parent to player's backpack
end) --ends the player joined function

If you want to make the event not a bindable event, that's fine, just make sure the server gets a signal to replace the tool.

Answer this question