Remove from local player inventory not working

(I’m quite new to scripting but im having ago)
What im trying to achieve is When you click the Gui button if you have a “Gem” in your inventory it will disappear. the error in the output is that there is no “starterpack”

local player = game.Players.LocalPlayer
local Gem = game.ReplicatedStorage.Gem
script.Parent.MouseButton1Click:Connect(function()
	if player.starterpack.Gem then
	Gem:Destroy()		
	end		
end)

I don’t know if these are necessary but just in case:
Video:

Image:


The selected script is the one im talking about

1 Like

Try this:

if player.Backpack.Gem then
    Gem:Destroy()		
end

Unless you stored your own folder called starterpack, it won’t work.

It’s called player.Backpack, not starterpack.

 if player.Backpack.Gem then
 Gem:Destroy()
 end

It just says this image
Even though its in my toolbar

Perhaps a:

if player.Backpack:FindFirstChild(“Gem”) or player.Character:FindFirstChild(“Gem”) then
Gem:Destroy()
end
2 Likes

It’s not doing anything now and nothing is coming up in the output

Use print statements to determine where it errors.

Also try doing:

local Gem = game.ReplicatedStorage:WaitForChild(“Gem”)

It looks to me (from the video) that it’s not being cloned, but parented to the player’s backpack, therefore your “Gem” variable will be trying to locate a non existent gem as it was moved to the player’s backpack.

1 Like

PLEASE USE THE OTHER CODE I WROTE BELOW! THIS IS ONLY FOR REFERENCE OF THE PROBLEMS IN YOUR CODE!

The reason it’s not destroying the gem is that you’re just saying Gem:Destroy() which is linked to the local Gem = ... before your mouse click event(you’d just be destroying the gem that was in ReplicatedStorage). Try something like this

for _,v in next, player.Backpack:GetChildren() do
	if v.Name == "Gem" then
		v:Destroy()
	end
end

for _,v in next, player.Character:GetChildren() do
	if v.Name == "Gem" then
		v:Destroy()
	end
end

The reason you check if it is in the character is that when you equip a tool, it goes into your character. I also noticed that you said starterpack which is not what it’s called when replicated to the player. It’s called Backpack.

The problem is that you’ve done it through a LocalScript. Use a RemoteEvent to connect with a script that will remove it. For example:

LocalScript.

local button = script.Parent
local plr = game.Players.LocalPlayer
local event = game.ReplicatedStorage.event -- The event name

button.MouseButton1Click:Connect(function()
	
	event:FireServer(plr)
	
end)

Script:

game.ReplicatedStorage.RemoteRemoval.OnServerEvent:Connect(function(plr)
	
	local GetTools = plr.Backpack:GetChildren()
	local HeldTools = plr.Character:GetChildren()
	
	if GetTools then
		
		for i,v in pairs(GetTools) do

			if v.Name == "Gem" then
			    v:Destroy()
                        end

			
		end
		
	end
	
	for i,v in pairs(HeldTools) do
		
		if v:IsA("Tool") and v.Name == "Gem" then -- Assures it is a tool and his name is "Gem"
			
			v:Destroy()
			
		end
		
	end
	
end)

And it works.

The problem isn’t the local script rather than the code flaws. You can delete things in your backpack and the localscript wouldn’t care less because it’s local with the client so as long as it’s a client script than it’s fine but you do make a good point.

This will only sell a single gem, not all of the gems a player has. If you have multiple gems, it will remove only one of them.

1 Like

Doing it through LocalScript will not display it on the leaderboard and will practically not assist in any way. For example: Let’s say you want to purchase a sword. You can see you have enough but the server does not see it. You come to purchase it and the server checks it. The purchase will fail.

As well, it will make it laggy to other players because the client (local player) will continue to get gems and then remove them by a local script while the server keeps the object and thus increasing the usage of memory.

I’m aware but he does not mention a leaderboard. But, for your sake replying to me over and over again I will make a new post.

You can check out the valid points I made in this post but you should do it over the server using remote events. I won’t explain what they are (go here for that) but I will explain how to program them for this action.

First, create your RemoteEvent in ReplicatedStorage and call it whatever you want just be sure to edit it in the scripts. I’m going to call it GemThing as a placeholder.

In the local script, you want to write your code to fire the remote event so the server can pick it up. Do this by writing this program:

local event = game.ReplicatedStorage.GemThing -- Change GemThing to your event name.

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

And for the server to detect this we want this program in a ServerScript in ServerScriptService. Not a LocalScript a normal script

local event = game.ReplicatedStorage.GemThing -- Change GemThing to your event name.

event.OnServerEvent:Connect(function(fired)
	--[[The argument "fired" is the player who clicked the button so it acts the same
	as "player" and it's always the first argument of OnServerEvent.]]
	
	for _,v in next, fired.Backpack:GetChildren() do
		if v.Name == "Gem" and v:IsA("Tool") then
			v:Destroy()
		end
	end

	for _,v in next, fired.Character:GetChildren() do
		if v.Name == "Gem" and v:IsA("Tool") then
			v:Destroy()
		end
	end
end)
1 Like

How StarterPack works, is that any gear that you put in there will be cloned to the Backpack of the LocalPlayer. Using the StarterPack basically saves you from having to write a script that clones stuff to the Player’s Backpack.
However, StarterPack is NOT a child of Player.

1 Like