Server script returning nil to something that exists

Hello!
I have been working on my game, and I have come across a problem…
I kind of need to equip weapons to yaknow, fight.

However, when I try to send data to the server, it just recieves nil.
Heres my module script:

tool = tool:Clone()
print(tool) --prints the tool name correctly (Fists)
equipEvent:FireServer(character, tool, idle, equip)

Heres the server script:

equipWeapon.OnServerEvent:Connect(function(player, character, tool, idle, equip)
	print(tool) -- prints nil, then returns an error later on in the script because the server script has no idea what tool is
end)

Any help is appreciated.

I think this should fix it

tool = tool:Clone()
print(tool.Parent.Name) --prints the tool name correctly (Fists)
equipEvent:FireServer(character, tool.Parent, idle, equip) -- you need to use .Parent if you clone something
equipWeapon.OnServerEvent:Connect(function(player, character, tool, idle, equip)
	print(tool.Name) -- prints nil, then returns an error later on in the script because the server script has no idea what tool is
end)

Try replacing the character by the tool, and the tool by the character.

ServerScriptService.Scripts.ItemScripts.Show:20: attempt to index nil with 'Name'
Nope. script still says its nil.

Like this?
equipEvent:FireServer(tool, character, idle, equip)
equipWeapon.OnServerEvent:Connect(function(player, tool, character, idle, equip)

1 Like

Nope. still doesn’t work. Returns the same error I had before

if tool == nil then
	print("tool wasnt defined")
end

Added this part to my script. It prints out “tool wasnt defined”. Obviously the script isnt getting the data from the client for some reason.

The problem should be from this line

tool = tool:Clone()

Try changing name of the variable to something else like this

toolClone = tool:Clone()

Because you are trying to clone itself when there is nothing in this variable yet.

1 Like

Same error. Tool is still nil.
tool wasnt defined - Server - Show:21

Where is your tool? Please make sure that you reference to the right location of tool

It means that you didn’t reference the right location of the tool, basically, you cloned nothing.

Main module gets its information from client

Client:

local fistsTool = characterWeaponsFolder.Fists 
mainModule.equipTool(player.Character, fistsTool, fistsTool.Anims.Idle, fistsTool.Anims.Equip) 

Module:

function module.equipTool(character, tool, idle, equip)
	local toolClone = tool:Clone()
	print(toolClone)  
	local equipEvent = game:GetService("ReplicatedStorage").Events.RemoteEvents.Arms.ShowWeapon
	equipEvent:FireServer(toolClone, character, idle, equip) 
end

Server:

equipWeapon.OnServerEvent:Connect(function(player, tool, character, idle, equip)
	if tool == nil then
		print("tool wasnt defined")
	end
end)

Tool is correctly defined.
Obviously theres more code in client, module, and server scripts. But I dont need to add them as it adds no information to my problem

The reason why the tool is nil on the server is because any instance (in this case, the tool) created/cloned on the client obviously wouldn’t (and shouldn’t) ever exist on the server, why are you cloning the tool on the client anyway?

That makes more sense now. This is the problem that I also encounter when I was developing my experience that you cannot send the tool model from client to server. I managed to solve this by sending the name of the tool and let the server finds it manually inside ServerStorage

The server should have the tool inside ServerStorage you simply get the name of weapon that the player wants to equip, then you can use for loop to find the tool, and equip it to the player that way.

2 Likes

I am cloning it so the player can equip it.

But why specifically on the client? Why not just do it on the server instead?

Because that was obviously my problem… I was doing stuff on client that should be on server

The downside of this approach is that if the player manage to change the name parameter to OP tool, then the player can equip whatever they want. You can fix this by checking from the client-side first before sending the request. Also, you can again check if players has that tool with them on server-side.

How do I check it from client side?

It is returning nil because the tool you cloned on the client doesn’t exist on the server.

I will add some code so you can get started.

-- CLIENT SIDE
local player = game.Players.LocalPlayer
local function equipTool(toolName)
       local toolModel = player.toolFolder:FindFirstChild(toolName)
       if toolModel == nil then
              return      -- perform client-side checking
       end
       
       equipToolRemote:FireServer(toolName)
end

-- END CLIENT SIDE CODE

-- SERVER SIDE
equipToolRemote.OnServerEvent:Connect(function(player, toolName)
           -- Guarding
           if game.ServerStorage:FindFirstChild(toolName) == nil then     -- Guard 1
                  warn("No such tool.") 
                  return
           end
           local playerToolFolder = player:FindFirstChild("ToolFolder")
           if playerToolFolder:FindFirstChild(toolName)  == nil then        -- Guard 2
                  warn("The player doesn't own that tool")
                  return
           end

           -- Equip Tool Logic from here.
end)

-- END SERVER SIDE CODE

Noted that you still have to change something inside this code. This code is not perfect, it just give you an idea how it should be solved.

2 Likes