I am making a weapon select GUI and it wont put the gun in the players backpack and instead gives that error.
Is anything wrong with my script?
Local:
local player = game.Players.LocalPlayer
local CollectionService = game:GetService("CollectionService")
local TaggedParts = CollectionService:GetTagged("Class")
for _, TaggedPart in pairs(TaggedParts) do
TaggedPart.MouseButton1Down:Connect(function()
if TaggedPart.Name == "Primary" then
local PrimaryGuns = game.Lighting.Primary:GetChildren()
local Primary = PrimaryGuns[math.random(1,#PrimaryGuns)]
local class = Primary
workspace.Events.SelectClass:FireServer(player, class)
end
end)
end
Have you printed the Primary to make sure that it’s not returning nil? It won’t give an error even if it doesn’t return anything when you’re getting a value from a table. Also, for script optimization purposes, you could just remove the local class in the LocalScript and use Primary instead.
I printed the Primary and it prints the name of the tool it picked so that works fine, and I don’t remove the local class because there is other weapon layouts too so if someone picks another one it makes the Class the layout they chose.
And the script that handles the OnServerEvent is in Workspace, Events Folder, Inside the event
I’m not sure if putting the script into ServerScriptServices does any difference, but it doesn’t hurt to try. But, now analyzing your code. It’s because you’ve passed the player as a parameter… when you don’t need to, in the LocalScript.
You have to include it in the server event function since it always returns the player who’ve fired the event regardless. So, at this current time, player in your script would be another parameter. All you need to do is pass the other arguments, which would be “class”. So, just remove the player in the :FireServer function and it should work.