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.
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.
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?
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).
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.
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)