How do I send a custom veriable through a server event?

  1. What do you want to achieve? Keep it simple and clear!

So I have been currently working on fps system that uses modules to change weapons. I have the clinet side down, but I need to send the current module information to the server script so I can change the weapon or fire a bullet.

  1. What is the issue? Include screenshots / videos if possible!

I have a script done for the server event, however my server script code is not picking up the veriable form the client script to require the correct module. I will put this code down below as an example:

This is the client script

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local tweenservice = game:GetService("TweenService")

local mouse = player:GetMouse()

local camera = game.Workspace.CurrentCamera
local GunEvent = game.ReplicatedStorage.RemoteEvents:FindFirstChild("GunEvent")

local framework = {
	inventory = {
		"ShanseiC44";
		"RaptorL28";
		"PowerDrill";
	};
	
	module = nil;
	viewmodel = nil;
	currentSlot = 1;
}

-- Weapon Framwork Function --

function Loadslot(Item)
	local viewModelFolder = game.ReplicatedStorage.ViewModels
	local moduleFolder = game.ReplicatedStorage.Modules
	
	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
	
	if moduleFolder:FindFirstChild(Item) then
		framework.module = require(moduleFolder:FindFirstChild(Item))
		
		GunEvent:FireServer(Item)
		
		if viewModelFolder:FindFirstChild(Item) then
			framework.viewmodel = viewModelFolder:FindFirstChild(Item):Clone()
			framework.viewmodel.Parent = camera
		end
	end
end

Loadslot(framework.inventory[1])

And this is the server script

local RunService = game:GetService("RunService")
local GunEvent = game.ReplicatedStorage.RemoteEvents:FindFirstChild("GunEvent")
local remoteEvent = game.ReplicatedStorage.RemoteEvents:FindFirstChild("ShotEvents")

local Module = nil


GunEvent.OnServerEvent:Connect(function(item)
	for i, v in pairs(game.ReplicatedStorage.Modules:GetChildren()) do
		if v.Name == item then
			Module = require(v)
			print("Module is loaded")
		else
			print("Could not find module")
		end
	end
end)

When i run the game it can’t find the module so it comes back with “could n ot find module”
I have a feeling I am not using the veriables in the function properly so I cam reached out to all you lovely coders to ask if you can see if I’ve made a mistake or how to make this work.

framework.module = require(moduleFolder:FindFirstChild(Item))

Im not sure if this is going to fix the problem, but FindFirstChild needs a string, not the module itself, so I think if you change it to Item.Name that may work. But im not sure.

1 Like

Possibly unrelated, but the first argument of the OnServerEvent function is the player that called :FireServer, it should actually be:

GunEvent.OnServerEvent:Connect(function(player, item)
2 Likes

Ah that makes sense. i will change that so it works properly thank you!

I think because the item function is taking from a table with a string I think it is ok. But I will definatly give it a go. I definately think the strinjg might be part of the problem though.

Thank you so much I would have completely overlooked that mistake.

The OnServerEvent works now. I wouldn’t have thought the problem would have been that simple of a fix!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.