Hello fellow devforum users! I have recently been making an inventory equipping script and for some odd reason the items do not get transferred from the players inventory folder to the players backpack. All help is appreciated!
local plrs = game:GetService("Players")
local plr = plrs:GetPlayers()[1] or plrs.PlayerAdded:Wait()
local event = game.ReplicatedStorage.InventoryEvents.SwordEquipped
local btn = script.Parent
local event2 =game.ReplicatedStorage.InventoryEvents.SwordUnEquipped
local Inventory = plr:WaitForChild("Inventory")
event.OnServerEvent:Connect(function()
if Inventory:FindFirstChild("Katana") and btn.Parent.ToolName.Text == "Katana" then
Inventory:WaitForChild("Katana").Parent = plr.Backpack
elseif Inventory:FindFirstChild("Better Katana") and btn.Parent.ToolName.Text == "Better katana" then
Inventory:FindFirstChild("Better Katana").Parent = plr.Backpack
elseif Inventory:FindFirstChild("Blade of light") and btn.Parent.ToolName.Text == "Blade of light" then
Inventory:FindFirstChild("Blade of light").Parent = plr.Backpack
elseif Inventory:FindFirstChild("Merciless blade") and btn.Parent.ToolName.Text == "Merciless blade" then
Inventory:FindFirstChild("Merciless blade").Parent = plr.Backpack
elseif Inventory:FindFirstChild("Warriors katana") and btn.Parent.ToolName.Text == "Warriors katana" then
Inventory:FindFirstChild("Warriors katana").Parent = plr.Backpack
elseif Inventory:FindFirstChild("Wood Sword") and btn.Parent.ToolName.Text == "Wood Sword" then
Inventory["Wood Sword"].Parent = plr.Backpack
end
end)
You’re using OnServerEvent, so I assume this is a server script, in which case at the moment it’ll run plrs:GetPlayers() which will return an empty table (as this script will probably execute before the first Player has joined), at which point it’ll index said table [1] which will return nil because there’s no Players in the game yet. I assume you have an error somewhere saying something along the lines of Attempted to call WaitForChild on nil.
You should use:
plrs.PlayerAdded:Connect(function(plr)
end)
Or better yet, just use the ‘Player’ argument of the OnServerEvent function.
…but then again, I have no idea what btn is. You’re indexing it’s Parent.ToolName with .Text so is it an TextButton in a gui? If so then I’d anticipate this is a local script in PlayerGui, which would make the OnServerEvent the incorrect event listener…
Some more details would help me understand a lot more.
local event = game.ReplicatedStorage.InventoryEvents.SwordEquipped
event.OnServerEvent:Connect(function(plr, nameOfTool)
local Inventory = plr:FindFirstChild("Inventory")
if Inventory and Inventory:FindFirstChild(nameOfTool) then
Inventory[nameOfTool].Parent = plr.Backpack
end
end)
Make sure in your FireServer, you’re passing the name of the tool (so btn.Parent.ToolName.Text)
Give this a try and see what happens, and just fyi, this server script should be on the server (i.e. in ServerScriptService).
No problem, and I’m glad it’s working for you now. Just to give a brief explanation of what I think was going wrong for you or anyone else who stumbles across this answer:
LocalScripts are designed to run on the client, and Scripts are designed to run on the Server. If you try to run one in the wrong place, it can either not run, or cause a bunch of problems.
Roblox uses a filtering system where, in order to prevent exploiters from changing the game, they filter any changes the client makes to their own game and won’t replicate it to the server or any of the other clients. This is great, because it means an exploiter can (for example) delete the entire map but in reality its only their game where the map gets deleted, and for the server and everyone else nothing has changed.
This also comes with issues however. Say you want, to use your example, move a tool into your backpack. You could do it on the client through a LocalScript, however for the server and anyone else in the game that tool hasn’t been moved into your backpack. This is where RemoteEvents and RemoteFunctions come into play. We can use them to bridge the gap between client and server by sending very specific packets of information between the two, and give very specific instructions to the server/client to undertake pre-made commands with that information (in this case, move a tool into the backpack).
Your issue was (I believe) that your server script was inside the Gui. The gui (and its descendants) is loaded and controlled on the client, so when you used FireServer(), you’re actually trying to send information from the client to itself. This naturally comes with a host of problems which were probably what you were experiencing.
Thanks for the explanation! It’s nice to get a quick solution and learn why the previous code failed. That way I will likely never make this mistake again. Thanks for the help! Have a nice day!