Why do I need this?
Simply to keep track of which pets were created first by incrementing a value for each player.
My goal is to make sure that race conditions can’t occur when reading and writing a shared variable using remotefunctions.
What did I try out
To make a simple test I created a connection between client and server using a remotefunction and updated a shared variable.
Server
local RemoteFunction = game.Workspace:WaitForChild("RemoteFunction")
local counter = 1
RemoteFunction.OnServerInvoke = function(player,clientData)
print(clientData," global=",counter )
counter +=1
end
Client Script 1
local RemoteFunction = game.Workspace:WaitForChild("RemoteFunction")
for i = 1,50 do
coroutine.wrap(function(j)
local streng = "script1 | local="..j.." | "
RemoteFunction:InvokeServer(streng)
end)(i)
end
Client Script 2
local RemoteFunction = game.Workspace:WaitForChild("RemoteFunction")
for i = 1,50 do
coroutine.wrap(function(j)
local streng = "script2 | local="..j.." | "
RemoteFunction:InvokeServer(streng)
end)(i)
end
Results?
It seems like it is single threaded and even if the localscripts are enabled they execute after the other script is finished. It also seems like there are no race conditions.
Question
Can I guarantee this behaviour of not race conditions (even when the iteration loop is very large)?
Can this guarantee that I get a unique counter for each pet
And yes this is just theoretical. I’ll use a UUID instead for the pet names, but was just curious about this behaviour