Code works in Test Server (2player) and in play solo not ingame

I’ve ran into this issue where my code works in play solo and a test server (2 players) but, magically doesn’t work ingame. Not a single error or warning occurs and I have no idea why.

I know that it’s an error happening server side since the client works just fine.

Here’s roughly what my code looks like:

-- Server Script
bindEvent("loanGunToCharacter",function(player,name)
		local w = WeaponsFolder:FindFirstChild(name):Clone()
		w.Parent = game.Workspace:WaitForChild(tostring(player.UserId)).ViewportCharacter
		if currentGunMappings[player.UserId] then
			table.insert(currentGunMappings,player.UserId,name)
			return
		end
		table.insert(currentGunMappings,player.UserId,name)
	end)
	
	bindEvent("updatePlayerBody",function(player,data)
		if playersData[player.UserId] then
			table.insert(playersData,player.UserId,{name=player.Name,info=data})
			return
		end
		table.insert(playersData,player.UserId,{name=player.Name,info=data})
	end)
	
	-- Update every player movement
	RunService.Heartbeat:Connect(function(step)
		if #playersData <= 0 then
			repeat wait() until #playersData > 0
			warn("Should be fine now")
			return
		end
		for i,v in pairs(Players:GetPlayers()) do
			local Character = v.Character
			--print("Character : ",Character)
			
			
			local success,err = pcall(function() -- bro what
			
				-- preload data
				Character["LeftUpperArm"]["LeftShoulder"].C0 = playersData[v.UserId]["info"]["LeftShoulder"]
				Character["RightUpperArm"]["RightShoulder"].C0 = playersData[v.UserId]["info"]["RightShoulder"]
					
				Character["LeftLowerArm"]["LeftElbow"].C0 = playersData[v.UserId]["info"]["LeftElbow"]
				Character["RightLowerArm"]["RightElbow"].C0 = playersData[v.UserId]["info"]["RightElbow"]
			end)
		
			if not success then
				warn("bruhhh")
			end
		end
	end)
-- Local Script
function sendCharacterMovementUpdate()
		local data = {
			["LeftShoulder"] = Character["LeftUpperArm"]["LeftShoulder"].C0,
			["RightShoulder"] = Character["RightUpperArm"]["RightShoulder"].C0,
			
			["LeftElbow"] = Character["LeftLowerArm"]["LeftElbow"].C0,
			["RightElbow"] = Character["RightLowerArm"]["RightElbow"].C0,
			
			["WeaponCFrame"] = Character:WaitForChild(PrimaryGun):GetPrimaryPartCFrame()
		}
		remotesFolder:WaitForChild("updatePlayerBody"):FireServer(data)
		data = {}
	end
	
	RunService.Heartbeat:Connect(sendCharacterMovementUpdate)

It’s supposed to mirror the Motor6D cframes from client to server. This way any changes made locally can be seen by everyone on the server. Unfortunately, only changes take place on the client whereas any other character has no changes made.

I’m absolutely stumped on how to fix this and I greatly appreciate any responses.

1 Like

Temporarily comment out the serverscript pcall and just straight up call the function. That way you will get stack trace and an error.

I get a [22:21:54.193 - ServerScriptService.Server2:74: attempt to index nil with 'info'] when doing a two player server in studio. Play solo yields no errors and ingame yields no errors. Line 74 is the code snippet that I quoted.

In this case, it looks like playersData[v.UserId] doesnt exist

Since that function is connected to the heartbeat, it is possible that it is executing before playersData[v.UserId] has been initialized elsewhere in the code. Perhaps add some sort of verification beforehand, such as

if playersData[v.UserId] == nil then return end

The error previously stated has been suppressed. However, functionality still doesn’t work. Works in play solo and 2 player test server. Fails to work ingame. No errors, no warnings.

It’s possible that playersData[v.UserId] is never getting initialized, perhaps due some error related to longer loadin times on an actual server startup?

I would create some function to verify that playersData[v.UserId] is being initialized correctly. Perhaps just print out that info to the console every second.

I feel like load times are a bad estimate to base performance off of. Roblox Servers tend to be less than performant at any given time.

As for checking, I got interesting results. It turns out that the code never actually runs. I put in a warn statement and nothing was output’ed out. I’m not sure what could be causing this to fail.

Edit #1: I’ve narrowed it down to this:

if #playersData <= 0 then
	repeat wait() until #playersData > 0
	warn("Should be fine now")
	return
end

Edit #2: I found the error and removed this completely. Seems to work perfectly now.

2 Likes

Glad to hear, remember to mark the solution so it can help others with a similar problem!

1 Like