Syncronization between client and server issue

I’m currently making an FPS framework for a fun little project or whatever.
I’m running into an issue but before i tell you let me explain the structure a little

When the player joins the game they are assigned a default table (named framework) with their userID that is nil until a weapon is equipped. When the weapon is equipped a module script or client can update 3 things. The module, viewmodel, and currentSlot. Because its updated on the client, i have a remoteEvent to sync the server framework to the client version.

ISSUE
mi trying to retrieve a players framework using another remotefunction
The issue is that the returned framework is default and doesn’t include any of the updated information. (retrieved on a client)
This is strange because when i print the servers version it is correct and updated, but just trying to retrieve that has become a struggle.

here is some code examples

SERVER:

local playerFrameworks = {}

local function createPlayerFramework(player)
	playerFrameworks[player.UserId] = {
		inventory = {
			'AR-15', -- Primary
			'Brick'  -- Secondary
			-- More items can be added here
		},
		module = nilVALUE,
		viewmodel = nilVALUE,
		currentSlot = nilVALUE
	}
end

local function updateFramework(player, playerFramework, viewmodelName)
	local framework = playerFrameworks[player.UserId]
	if framework then
		framework = playerFramework
		framework.viewmodel = viewmodelName
		print(framework)
	end
end

UpdateFrameWork.OnServerInvoke = function(player)
	return playerFrameworks[player.UserId]
end

As you can see it correctly returns the framework, creates new ones, and updates it.

CLIENT TRYING TO RETRIEVE:

if input.KeyCode == Enum.KeyCode.One then
		if db then return end
		frameWork = UpdateFrameWork:InvokeServer(player)
		db = true
		if frameWork.currentSlot ~= 1 then
			print("loading 1st slot, which we dont have currently")
			print(frameWork.currentSlot)
			viewModelController:loadSlot(frameWork.inventory[1], 1)
			frameWork.currentSlot = 1
		elseif frameWork.currentSlot == 1 then
			viewModelController:dequipSlot()
			frameWork.currentSlot = nilVALUE
		end
		task.wait(.5)
		db = false
	end

As you can see better the issue is that if i currently have equipped slot 1, and i press 1 to DEequip it, it prints that the value of currentSlot is actually “NilString” (my default value for it) so it runs the 1st if statement meaning I’m just equipping the same weapon over and over again. aka the table is not correctly updated somehow.

Where is this function used exactly?

Also I am not able to find the exact definition of nilVALUE anywhere in the script which you provided.

Here is the entire server script (excluding variables) sorry about nilVALUE, its currently just a place holder for nil because when i set things to nil they tend to have issues and disappear. its just a custom nil basically.

local nilVALUE = "NilString"
--INITIALIZE THE TEMPLATE TABLE

local playerFrameworks = {}

local function createPlayerFramework(player)
	playerFrameworks[player.UserId] = {
		inventory = {
			'AR-15', -- Primary
			'Brick'  -- Secondary
			-- More items can be added here
		},
		module = nilVALUE,
		viewmodel = nilVALUE,
		currentSlot = nilVALUE
	}
end

local function updateFramework(player, playerFramework, viewmodelName)
	local framework = playerFrameworks[player.UserId]
	if framework then
		framework = playerFramework
		framework.viewmodel = viewmodelName
		print(framework)
	end
end

UpdateFrameWork.OnServerInvoke = function(player)
	return playerFrameworks[player.UserId]
end

game.Players.PlayerAdded:Connect(function(player)
	createPlayerFramework(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	playerFrameworks[player.UserId] = nil
end)

UpdateInventory.OnServerEvent:Connect(updateFramework)

also i know a lot of these could just be :Connect and whatever

Shouldn’t this function update the framework instead of returning it?

the names are probably confusing you. Thats my bad, ive been going thru different methods of doing this so its a little messy.

This updates the framework values:


local function updateFramework(player, playerFramework, viewmodelName)
	local framework = playerFrameworks[player.UserId]
	if framework then
		framework = playerFramework
		framework.viewmodel = viewmodelName
		print(framework)
	end
end

This sends a copy of the current framework:

UpdateFrameWork.OnServerInvoke = function(player)
	return playerFrameworks[player.UserId]
end

sorry about the confusing names

Once again this was a pretty easy fix. (I’m not that smart apparently)

I will post the solution incase anyone needs it.
the problem was that i wasnt updating the actual table but just our local reference to it
anyways heres the solved code

local function syncFramework(player, playerFramework, viewmodelName)
	local framework = playerFrameworks[player.UserId]
	if framework then
		for key, value in pairs(playerFramework) do
			framework[key] = value
		end

		framework.viewmodel = viewmodelName
	end
end

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