How to remove tool from Player StarterGear?

What do you want to achieve?
I’m using a DataStore for the player’s backpack so that it saves after leaving the game & dying; however, one of the tools I have I’d like to save if it’s not been destroyed but if it has been destroyed, then removed from the Player’s StarterGear.

What is the issue?
Video won’t upload on here…
here’s the link to the Vimeo: InventoryIssue on Vimeo

Tool Script:

local tool = script.Parent
local drink_sound = script.Parent.DrinkSound
local clicked = false
tool.Activated:Connect(function()
	if clicked == false then
		clicked = true
		drink_sound:Play()
		local humanoid = tool.Parent:FindFirstChild("Humanoid")
		humanoid.Health = humanoid.Health + 10
		wait (2)
		tool:Destroy()
	end
	end)

Screen Shot 2022-07-06 at 6.20.51 PM

StarterGear while playing:
Screen Shot 2022-07-06 at 6.20.12 PM

Nothing inside StarterPack:
Screen Shot 2022-07-06 at 6.21.21 PM

1 Like

You can make your code access Player.StarterGear. Assuming the code you’ve shared is client-sided (a LocalScript), you can do:

-- Services
local Players = game:GetService('Players')

-- Accessing the local player
local Player = Players.LocalPlayer

local tool = script.Parent
local drink_sound = script.Parent.DrinkSound
local clicked = false
tool.Activated:Connect(function()
	if clicked == false then
		clicked = true
		drink_sound:Play()
		local humanoid = tool.Parent:FindFirstChild("Humanoid")
		humanoid.Health = humanoid.Health + 10
		task.wait (2)
		
		-- Find tool in StarterGear
		local StarterTool = Player.StarterGear:FindFirstChild(tool.Name)
		if  StarterTool then
			StarterTool:Destroy()
		end
		-- Destroy tool in hands
		tool:Destroy()
	end
end)

Hope this helps!

2 Likes

Thanks for the helpful reply! :slight_smile:

I’ve changed the code to the code you’ve provided but now the tool is not being destroyed in the player’s hands. It plays the audio and heals the player but doesn’t get removed from the backpack, hands, and StarterGear of the player.

I copied the code exactly and also tried replacing

local StarterTool = Player.StarterGear:FindFirstChild(tool.Name)

with

local StarterTool = Player.StarterGear:FindFirstChild("+10 Health")

EDIT:
Also, the script within the tool is not a LocalScript—should I make it a local script instead?

I’ve edited the code to be compatible with Server-ran code. Try this :grin:

-- Services
local Players = game:GetService('Players')

local tool = script.Parent
local drink_sound = script.Parent.DrinkSound
local clicked = false
tool.Activated:Connect(function()
	if clicked == false then
		clicked = true
		drink_sound:Play()
		-- Accessing the Player
		local Player = Players:GetPlayerFromCharacter(tool.Parent)
		
		local humanoid = tool.Parent:FindFirstChild("Humanoid")
		humanoid.Health = humanoid.Health + 10
		task.wait (2)
		
		-- Find tool in StarterGear
		local StarterTool = Player.StarterGear:FindFirstChild(tool.Name)
		if  StarterTool then
			StarterTool:Destroy()
		end
		-- Destroy tool in hands
		tool:Destroy()
	end
end)

A little explanation on what I changed:
LocalScripts can access Players.LocalPlayer because they are ran on the client. Since your script is a Server script, it cannot access this reference. I’m now using the GetPlayerFromCharacter method to get to the Player object instead. Hopefully this now works :slightly_smiling_face:

1 Like

I made it a LocalScript and your code worked perfectly!

Thank you :heart:

1 Like

WOW! You truly went above and beyond :slight_smile:

I appreciate it soooo much!

1 Like