I’ve tried searching for this but I’m not sure how to figure this out. Help
My current code:
local rs = game.ReplicatedStorage
script.Parent.Touched:Connect(function(Hit)
if Hit then
if Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
local collect = rs.FGTeeV:Clone()
collect.Parent = Player.Backpack
script.Parent:Destroy()
else
script.Parent:Destroy()
end
end
end)
you could compare the Instance.name of the two items.
A server-side Script example:
local item1 = game.ReplicatedStorage.ItemOne
--'player' is the player Instance
local backpack = player.Backpack
if not backpack:FindFirstChild(item1.Name) then
print("Not in backpack")
--code to put it in
else
print("In backpack")
end
Bare in mind this is when adding a tool to the backpack.
I assumed it was a server-side script. Sorry if it isn’t. LocalPlayer only works in a LocalScript.
So, you might have:
local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
Then, continue the comparison from there.
Make sure to use WaitForChild() when referencing a child of an instance, so that the client has time to register it.
However, remember anything edited client-side will only change for that player, and will not be registered server-side. You can use a RemoteEvent to communicate between client and server.
The server-side example I sent uses the player’s Player object, whether it is obtained from a RemoteEvent, a part being touched, or something else. The Backpack is where the player’s unequipped tools are stored. The script I sent is to be used when you are trying to put an item into the Backpack. All it does is check to see if a tool of the same name already exists in the Backpack, which essentially is checking for the tool already in there. Just make sure no other tools have the same name.
The FindFirstChild() returns true if it is in there, and nil if it is not. Putting a not in front of it is basically saying ‘if it isn’t in there already’.