How to check if the tool is NOT a child of the player's backpack/startergear

hello i come to you with a problem that i can’t cope with.
namely (as the title says) I cannot check with the server script whether the tool is not in the player’s backpack or StarterGear.

I see this on the console: ‘Sword is not a valid member of StarterGear “Players.dkdntgn.StarterGear” - Server - Script:2’

I appreciate any help

script.Parent.GetTool.OnServerEvent:Connect(function(plr)
	if game.ReplicatedStorage.Tools.Sword.Parent ~= plr.StarterGear then
		game.ReplicatedStorage.Tools.Sword:Clone().Parent = plr.StarterGear
		wait()
		plr:LoadCharacter()
	else
		plr.StarterGear.Sword:Destroy()
	end
end)
1 Like
script.Parent.GetTool.OnServerEvent:Connect(function(plr)
	if not game.ReplicatedStorage.Tools.Sword.Parent == plr.Backpack then
		local clone = game.ReplicatedStorage.Tools.Sword:Clone()
                clone.Parent = plr.Backpack
		wait()
		plr:LoadCharacter()
	else
		plr.Backpack.Sword:Destroy()
	end
end)

this line checks if the parent of the sword in replicated storage is StarterGear, which doesnt really make sense to me since you already know its in replicated storage

1 Like

i tried this, but that doesn’t work either

if not plr.StarterGear.Sword then

like, the tool that you wanna check its parent, what is it

You’re checking if the tool that is already parented to ReplicatedStorage is somehow parented to StarterGear, which is impossible. Instead you should check if there is a child of the StarterGear called Sword, using :FindFirstChild().

script.Parent.GetTool.OnServerEvent:Connect(function(plr)
    if plr.StarterGear:FindFirstChild("Sword") then
        plr.StarterGear.Sword:Destroy()
    else
        game.ReplicatedStorage.Tools.Sword:Clone().Parent = plr.StarterGear
        task.wait()
        plr:LoadCharacter()
    end
end)

FindFirstChild | Roblox Creator Documentation

1 Like

it is classic sword from roblox

it works! thanks and by the way I have a question if the same script would work WaitForChild() ?

WaitForChild() yields the script until a certain child is loaded, so it would not be useful in this context. FindFirstChild() is used in this script to check if a child exists, not wait for one to exist. In this context it’s not just a case of replacing FindFirstChild with WaitForChild, but you can read more about that in the Creator Documentation link that I listed in my post above.

1 Like