2 people firing an event at the same time

I have a gui that if clicked, will fire an event to the server, and save some data, however if 2 people click the gui at the exact same time, sometimes 1 person’s data becomes the other person’s data, and the 2nd person’s original data is lost, I’m not sure what Im doing wrong. Here is the code for the gui:

yes.MouseButton1Up:Connect(function()
	print(runtime)
	st:FireServer(runtime)
	frame.Visible = false
	timedis.Visible = false
end)

Server:

st.OnServerEvent:Connect(function(player, runtime) 
	playertime = tonumber(runtime)  
	print("before"..playertime)
	playertime *= 100
	playertime = math.floor(playertime + 0.5)
	print("after"..playertime)   
	local userid = player.UserId
	local key = userid
	local success, err
	repeat
		waitfor(Enum.DataStoreRequestType.UpdateAsync)
		success, err = pcall(leaderboardstore.UpdateAsync,leaderboardstore,key,function()
			return playertime
		end)
	until success
	
	
	if not success then
		print(err)
		warn(err)
	end    
end)

You need to localize the playertime variable so juts do

local playertime = …

and that should fix it; otherwise the 2nd person overwrites the playertime variable

1 Like