Getting "nil" in output

local selected = script.Parent.Parent.Parent.Handler.Selected
local player = game.Players.LocalPlayer
local character = player.Character
local rs = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Click:connect(function()
	local swordName = script.Parent.Parent.Parent.Handler.Selected.Value
	print(swordName)

	if script.Parent.Text == "Equip" then
		local selectedSword = rs:FindFirstChild(swordName)
		print(selectedSword)

	else if script.Parent.Text == "Unequip"
		then
			
		end
	end
end)

There are no errors, however selectedSword prints as “nil”. How can I fix this?
I need it to say the name of the sword, so I can further script the part where it does what I want it to. However, I am stuck on this.

Output: https://gyazo.com/15acf3e1e0c3c824bc1dfcae2a0ccf80

Can you show me the ReplicatedStorage? The problems looks like it came from there as ReplicatedStorage doesn’t seem to have the value of swordName in it.

1 Like

Are you sure ReplicatedStorage has a “Child” named “Firebrand”?

1 Like

Maybe that “Firebrand” is not in ReplicatedStorage. Also, use elseif instead of else if.

1 Like

Here is the replicated storage: https://gyazo.com/1c8576db1f4598644974cb32ea5ec375
@Nube762 @BadlyDev and @Blockzez

To add on, the objective is to have an equip button which (once clicked) finds the tool by the name of the value of a StringValue by the name of “Selected”, and then clones it into the player’s backpack.

I’d suspect it hasn’t been loaded yet, try rs:WaitForChild(swordName) as it yields until it contains an instance with that name in the ReplicatedStorage.
Has it been there when you’re running the script? Is the image ReplicatedStorage when it’s running or not?

This is what I get:
https://gyazo.com/6e5868a2096c630807c20fde89bef9ac. I ran it before multiple times with different additions, however I was not able to get it working.

Try to get the name (string) of the instance swordName.Name, WaitForChild and FindFirstChild(?) when provided an Instance type converted it to string “Instance” then try and find to see does an Instance contain the name "Instance". print(swordName) prints it correctly and prints the Instance name because print prints the value converted using tostring (minor detail: in future update, it might call __tostring instead like in Lua 5.4) (instead of printing it as Instance).

Something like:
rs:FindFirstChild(swordName.Name)

1 Like

This wouldn’t work, as “swordName” is “script.Parent.Parent.Parent.Handler.Selected.Value”

My bad, didn’t read the top half you sent properly :sweat_smile:

You got it man, thanks a lot! All I had to do was use my brain, and I would have gotten it. Such a small thing escaped my mind. Thanks a lot man, I really appreciate your help.

Wait so would it not work? It prints both values correctly. Also, I’ll check by putting in the remaining code, and checking if it works.

Edit: It does work. Thanks a lot everyone.

Good to hear.

  • Although, I would personally organized your game a lot more.
    (E.g., folders for the tools, folders for Remote Events etc - It makes it a lot cleaner and easier to understand for when you start getting a lot)

  • Do the sword equipping on the server, rather than on the client (Unless your plan is to do that, but atm it seems your trying to do it all on the client)

  • Instead of using else if, make that elseif. (else if will still work ofc)

  • Instead of doing >if script.Parent.Text == “Equip” then< I would personally detect if that sword is current equipped on the server, by checking a table of the current sword equipped by the player.

(Note: I haven’t tested this, so not sure if it works properly, but something along these lines - Just wrote it up here)

local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Variable to ReplicatedStorage

local RemoteEvents = ReplicatedStorage.RemoteEvents -- Variable to ReplicatedStorage / Remote events 
local EquipUnequipEvent = RemoteEvents.EquipUnequipEvent -- Variable to ReplicatedStorage / Remote events / The equip/Unequip event

--[[
pls use folders to organize, ty
--]]

local PlayersWithSwordsEquipped = {}

EquipUnequipEvent.OnServerEvent:Connect(function(Player, Cmd, Sword)
	if Cmd == "Equip" and PlayersWithSwordsEquipped[Player.Name] ~= Sword then
		if PlayersWithSwordsEquipped[Player.Name] == nil then
			PlayersWithSwordsEquipped[Player.Name] = Sword
		end
		PlayersWithSwordsEquipped[Player.Name] = Sword

		--Do all of the sword equipping here

		return "Equipped"-- Return the results to the client, so they know it's been equipped and then change the text in the button to "Unequip"
	elseif Cmd == "Equip" or PlayersWithSwordsEquipped[Player.Name] == Sword then
		PlayersWithSwordsEquipped[Player.Name] = nil

		--Do all the sword unequipping here

		return "Unequipped"
	end
end)

To call this and to get a return / to see the return value on the client do something similar to this when you call it.

local Result = EquipUnequipEvent:FireServer("Equip", Child.Name) -- This will set the variable "Result" to the result of what the RemoteEvent / function has returned, for example if "Equipped" was returned, "Result" would be "Equipped"
print(Result)
1 Like

Alright, I’ll try it out. Thanks.