I am currently using MongoDB as my database for saving minutes when they join the game (this works). I want it to be able to work for different teams. So, when they are in Team1, the data will be saved on Team1’s database, for Team2 their data will be saved in Team2’s database, and so on. I am currently having difficulties with that, and I haven’t got any solutions from it.
If you have any code already then show it (of course you should not show the database secrets / database url) as most of us can’t help without some sort code.
Just make a dictionary with a key for containing minutes and a key containing a array filled with player’s in the same team, somethinf like:
local teamdata = {
Minutes = 0,
Players = {Player...}
}
Then backport/make a different function that saves the data
As of this, it’s working. I’ve tried using mongoose in the JavaScript code, but it doesn’t seem to work.
local http = game:GetService("HttpService")
local serverstorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local webhook = 'webhooklink'
local GroupID = 0
local RanksAllowed = 0
local prefix = "!"
local seconds
local minute
local minutes
local player = Players.LocalPlayer
local function getData(Player)
print("activity started")
if Player:GetRankInGroup(GroupID) >= RanksAllowed then
local response = http:GetAsync('databaselink')
local data = http:JSONDecode(response)
if data then
print(data.minutes)
local minute = data.minutes
end
while wait(60) do
updateData(Player.UserId)
end
end
end
function updateData(playerID)
local response = http:GetAsync('another link')
local data = http:JSONDecode(response)
local plrMinutes = data.minutes
print("Updating Data!")
plrMinutes = plrMinutes + 1
local data = {
minutes = plrMinutes
}
minutes = plrMinutes
local jsonData = http:JSONEncode(data)
http:PostAsync('post async link')
print("Finished Updating Data!")
end
function stop(Player)
print("Activity ended")
local Data = {
['username'] = 'Activity Tracker for Scientific Advancements';
['content'] = Player.Name .. "'s activity: " .. minutes .. " minute(s)"
}
print(Data) -- for testing purposes
Data = http:JSONEncode(Data)
http:PostAsync(webhook, Data)
end
BindableEvent.Event:Connect(getData)
BindableEventStop.Event:Connect(stop)
There is no authorization code yet for the MongoDB, I am planning to add it later.
const express = require("express");
const app = express();
const mongoose = require("mongoose");
mongoose.connect(`link of the mongoDB`);
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function () {
console.log("Connected!");
});
require("./model/player.js");
const playerModel = mongoose.model("Player");
app.get("/player-data/:id", async (request, response) => {
async function playerDataCheck() {
const playerData = await playerModel.findOne({
userID: `${request.params.id}`,
});
if (playerData) {
return playerData;
} else {
const newPlayerDataInstance = new playerModel({
userID: `${request.params.id}`,
minutes: 0,
});
const newPlayerData = await newPlayerDataInstance.save();
return newPlayerData;
}
}
response.json(await playerDataCheck());
});
const bodyParser = require("body-parser");
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
app.post("/player-data/update-minutes/:id", async (request, response) => {
await playerModel.findOneAndUpdate(
{ userID: `${request.params.id}` },
{ $set: { minutes: request.body.minutes } }
);
response.send("Updated Database.");
});
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});
The thing is the teams are in Roblox groups, so I don’t think I would be able to fill in all of the players per group.
https://developer.roblox.com/en-us/api-reference/function/Player/IsInGroup
local Players = game:GetService("Players")
local teams = {}
local groups = {
TeamA = 000000;
TeamB = 111111;
}
for _, plr: Player in next, Players:GetPlayers() do
for tname: string, gid: number in next, groups do
if plr:IsInGroup(tonumber(gid)) then
if not typeof(teams[tname]) == "table" then
teams[tname] = {}
end
table.insert(teams[tname], plr)
teamavail = true
break
end
end
end
Adding on to my question, I wanted that only the players get the minutes, not the team entire team. So, when one player does a command to start the stopwatch for the activity, and then stops it after a few minutes, it will save the minutes only for that player.
Then make a key on the teams dictionary, something named like “StopwatchStartPlr” and each time a player starts the stopwatch, assign them to that key. When the stopwatch ends, read the key and save data for that player. This can depend on your own implementation, theres 2 ways I could think of but I only listed one here for you.
That’s a good idea, I will try and implement this and will reply if it works or not.
Got it to work. I had to use different files in order to connect with MongoDB in the JavaScript folder. Thank you all for your help, though!