Problem writing into dictionaries

  1. What do you want to achieve?
    I’m trying to make a dictionary in the server script containing data which needs to be actively updated(dictionary ‘activeData’ in the server script below). For later development I used functions to update the data inside it.
    Currently the problem is getting camera cframes in real time, and I will only show related code here.
  2. What is the issue?
    Problem is the writing function does not work, without throwing errors or something, it just doesn’t write into it.
  3. What solutions have you tried so far?
    Searched the issue on the devforum just now, none matches.(Or is it too obvious and I didn’t even notice?)

So, here’s the related code in my server script:

--/ServerScriptService/EventInit
local rs = game:GetService("ReplicatedStorage") --line 1
--...
local activeData = { --line 26 , the aforementioned data dictionary
--...
	camCFrame = {},
--...
}
--...
function getCamCFrame(plr) --line 34 , the get method
	return activeData.camCFrame[plr.Name]
end
--...
function setCamCFrame(plr,cf) --line 44 , the get method
	activeData.camCFrame[plr.Name] = cf
	print(table.concat(activeData.camCFrame,',')) -- tried to print it, but did not print anything :(
end
--...
rs:WaitForChild("UpdateCamCF").OnServerEvent:Connect(function(plr,cf) --line 192 , update data through remoteEvent
	if not cf then return end
--	print(cf)                        --HERE it prints normally, meaning the data is transferred correctly
  	setCamCFrame(plr,cf)
--	activeData.camCFrame[plr.Name] = cf --yes i tried this as well, still nothing
	print(table.concat(activeData.camCFrame,',')) --again doesn't print anything
end)

rs:WaitForChild("RegisterBullet").OnServerEvent:Connect(function(plr) --line 200 , using the get method
	local round = getCurrentRound(plr)
--...
end) --line 208

And the code on the client:

--/StarterPlayer/StarterPlayerScripts/LocalInit
--...
local rs  = game:GetService("ReplicatedStorage") -- line 2
--...
local updcf= rs:WaitForChild("UpdateCamCF") -- line 22
-...
spawn(function() -- line 24 (I know corotines are better, will do that later)
	while true do
--... and don't worry wait() is called in these hidden lines
		updcf:FireServer(game.Workspace.CurrentCamera.CFrame) -- line 32
	end
end)

In the testing, both scripts reported no errors. No errors or warnings was reported in the output when attempting to write the data into the dictionary. When the scripts attempted to read the data, however, it says attempt to index nil, which is actually attempting to read with an invalid key.

Any help will be appreciated. :grin: Well if this is really just an obvious problem, feel free to point it out.

Can you specify what line the error is on?

Well the error was thrown when I attempt to call that getCamCFrame() at line 34 in the server script. I’m sure I have called the setCamCFrame() already but still no data was written.

Make sure you’re setting that index. Print it out and see if it really exists.

I’ve also converted your code

coroutine.resume(coroutine.create(function()
	while true do
		wait()
		updcf:FireServer(game.Workspace.CurrentCamera.CFrame) -- line 32
	end
end))

Also, don’t hide any lines. Hiding lines prevents any potentially corrupted code from being read. If you’re scared someone is going to use this against you just change some things around. Such as, remote names, argument orders, argument types, etc.

Also, Also, you don’t have to use table.concat. It is pointless here. Just print the values.

Also, pro-tip

workspace

is way easier and faster than writing

game.Workspace

workspace also has the advantage of game:GetService without having to use game:GetService.

Workspace is a property of game (the only service that does this), so there’s no difference in this case.

2 Likes

I didn’t say there was a difference just workspace has the advantage of not having to use game:GetService just incase someone renames workspace. And it’s faster than writing game.Workspace

Ok so this time I put the print line in the set function:

function setCamCFrame(plr,cf)
	print("Set index: " .. plr.Name)
	activeData.camCFrame[plr.Name] = cf
	print("Set result: " .. table.concat(activeData.camCFrame,','))
end

And here’s the output:

  Set index: Chrous_Pickaxe
  Set result: 

No clue why this happened :roll_eyes:

just print(cf) to make sure that works and get rid of concat and just print the raw value print(activeData.camCFrame[plr.Name])

Table.concat only works on arrays and not string-indexed entries in the table. Maybe you should try printing the value in the table directly:

function setCamCFrame(plr,cf) --line 44 , the get method
	activeData.camCFrame[plr.Name] = cf
	print(plr.Name, activeData.camCFrame[plr.Name])
end
1 Like

Is the problem on line 19? As in thats whats causing the error? I used your script in game and it told me line 19. Not 100% sure though.

He didn’t give the full script so it wouldn’t be right to test it in a new game unless posted. There could be some flaws with an edited version.

Hello! Try indexing the dictionary using numbers instead of strings to see if anything will print.

print( activeData.camCFrame[1] )

I’m back, sorry for being away for so long.
Well this helped me. I didn’t know concat doesn’t even work :sob: and actually what caused the error was that I forgot to change the index name

And specially thanks to @suspectshot108 your tips are of great value

1 Like