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