Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I delete a specific weapon from starter gear?

I have a program that is supposed to delete the weapon in hand and delete from starter gear when a button is pressed. It deletes from the hand properly, but when I reset, the weapon appears again. This makes me believe that it wasn't deleted from starter gear. What is the issue?

local function onCreatePartFired(player, tool)
    local SG = player.StarterGear
    if tool then
        local sg_tool = SG:FindFirstChild(tool.Name)
        if sg_tool then
            print("hey")
            SG:FindFirstChild(tool.Name):Destroy()
        end
        tool:Destroy()
    end
end

local Players = game:GetService("Players")
plr = game:GetService("Players").LocalPlayer

char = plr.Character or plr.CharacterAdded:Wait()

script.Parent.MouseButton1Click:Connect(function()
	
	local tool = char:FindFirstChildOfClass("Tool")
	onCreatePartFired(plr,tool)
	
end)

1 comment

0

for debugging purposes, "hey" is being print when I run the program

1 answer

✓ Accepted answer

When you are deleting it from your StarterGear on the client, the server will not know that they were deleted and will proceed to clone the tools every time you respawn as usual. You will need to use a RemoteEvent for this. Add a RemoteEvent to ReplicatedStorage before running my snippet.

-- LOCAL SCRIPT
local Players = game:GetService("Players")
plr = game:GetService("Players").LocalPlayer
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

char = plr.Character or plr.CharacterAdded:Wait()

script.Parent.MouseButton1Click:Connect(function()

    local tool = char:FindFirstChildOfClass("Tool")
    remote:FireServer(tool.Name)
end)


-- SERVER SCRIPT
local Players = game:GetService("Players")
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local function onCreatePartFired(player, toolname)
    local SG = player.StarterGear
    if toolname then
        local sg_tool = SG:FindFirstChild(toolname)
        if sg_tool then
            print("hey")
            sg_tool:Destroy()
        end
        
   if player.Character:FindFirstChild(toolname) then
	player.Character[toolname]:Destroy()
    end
end

remote.OnServerEvent:Connect(onCreatePartFired)

This discussion is preserved as it was. New answers, comments, and votes are closed.