Script doesnt move onto else statement

  1. What do you want to achieve?
    This script switches a tools parent from the player to a folder whenever an event is activated. Its designed to switch back and forth between the folder and player
  2. What is the issue?
    The script checks if the tool is inside the players workspace model, or if its inside the folder already. However the script stops whenever its checking the players model and doesnt move onto the else statement
local Replicated = game:GetService("ReplicatedStorage")
local UnEquip = Replicated.UnEquip


UnEquip.OnServerEvent:Connect	(function(plr,key,gunchoice)
	if game.Workspace:FindFirstChild(plr.name)[key] then -- the script continues even if theirs no tool in the players model
		local gun = game.Workspace:FindFirstChild(plr.name)[key] -- script stops here, instead of moving onto else
		gun.Parent = plr.gunstats
	else -- script never reaches here
		plr.gunstats:FindFirstChild(key) 
		local gun = plr.gunstats:FindFirstChild(key)
		gun.Parent = plr.Backpack
			end
	
end)

I think its because you did not capitalize N for Name. Capitalization matters, because if you did not add an uppercase or lowercase letter when you are supposed to, then it will just error. So better be careful next time.
Try using this code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UnEquipEvent = ReplicatedStorage:WaitForChild("UnEquip")

UnEquipEvent.OnServerEvent:Connect(function(player, key, gunchoice)
	if player.Character:FindFirstChild(key) then
		local gun = player.Character:FindFirstChild(key)
		gun.Parent = player.gunstats
	else
		local gun = player.gunstats:FindFirstChild(key)
		gun.Parent = player.Backpack
	end
end)

Thanks, This actually simplified alot of things i accidently went over, Like just getting the players character instead of searching the workspace for the players name lol. But i dont believe the capitalization in Name matters, as i have another script that refers to name as lowercase and still functions.
The script you made prints an error saying “Attempt to index nil with Parent” and brings you to this line
gun.Parent = player.Backpack

1 Like

When else is mentioned, it means the gun could be anywhere else (maybe already in the player’s backpack). Are you only checking if the gun is equipped? I believe you should also check if the gun is in the player’s backpack.

Try this code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UnEquipEvent = ReplicatedStorage:WaitForChild("UnEquip")

UnEquipEvent.OnServerEvent:Connect(function(player, key, gunchoice)
	if player.Backpack:FindFirstChild(key) or player.Character:FindFirstChild(key) then
		player.Character:WaitForChild("Humanoid"):UnequipTools()
		local gun = player.Backpack:FindFirstChild(key)
		gun.Parent = player.gunstats
	else
		local gun = player.gunstats:FindFirstChild(key)
		gun.Parent = player.Backpack
	end
end)

Whats the function for the gunstats folder/object you’re parenting the tool to?

Hey @goblinc0re , have you tried the code I have given above? If you have, any problems? If you havent, try using the code and see if it would work.