Running a Module Script for Each Player

Greetings,

I am currently working with a Module Script in my game that is responsible for managing various tasks. However, I’ve encountered an issue where the script operates globally for all players, rather than executing individually for each player.

So far, i have attempted to put the “TaskGroups” table inside the “checkGroups” function.

Task Module:

local playerService = game:GetService("Players")
local dataMod = require(script.Parent.Data)
local spawnParts = workspace.SpawnParts
local TasksMod = {}

local function getWins(winsNum)
	for _, winsPart in pairs(spawnParts:GetChildren()) do
		if winsPart.Wins.Value == winsNum then
			return winsPart
		end
	end
end

local TaskGroups = {
	checkPlayerInGroup = false;
	checkPlayerTime = false;
	checkPlayerWins = false;
}

TasksMod.checkPlayerInGroup = function(player)
	local groupId = 34325147

	if player:IsInGroup(groupId) then
		TaskGroups.checkPlayerInGroup = true
	end
end

TasksMod.checkPlayerTime = function(player)
	local startTime = tick() 

	while true do
		wait(1)
		local currentTime = tick()
		local timeInGame = currentTime - startTime

		if timeInGame >= 20 then
			TaskGroups.checkPlayerTime = true
			break
		end
	end
end

TasksMod.checkPlayerWins = function(player)
	local winsNum = dataMod.get(player, "Wins")

	if winsNum == 1 then
		TaskGroups.checkPlayerWins = true 
	end
end

local function checkGroups(player)
	TasksMod.checkPlayerInGroup(player)
	TasksMod.checkPlayerWins(player)

	coroutine.wrap(TasksMod.checkPlayerTime)(player)

	local allTrue = true
	for _, groupValue in pairs(TaskGroups) do
		if groupValue ~= true then
			allTrue = false
			break
		end
	end

	local output = ""
	for groupName, groupValue in pairs(TaskGroups) do
		output = output .. "{" .. groupName .. " = " .. tostring(groupValue) .. "}, "
	end

	output = output .. "{AllTrue = " .. tostring(allTrue) .. "}"

	print(output) 

	return allTrue
end

TasksMod.getOutput = function(player)
	return checkGroups(player)
end

playerService.PlayerAdded:Connect(checkGroups)

return TasksMod

Monetization Module:
The segment of the script that is crucial for invoking the output and conducting the checks.

local playerService = game:GetService("Players")
local dataService = game:GetService("DataStoreService")
local insertService = game:GetService("InsertService")
local marketService = game:GetService("MarketplaceService")
local spawnParts = workspace.SpawnParts
local dataMod = require(script.Parent.Data)
local tasksMod = require(script.Parent.Tasks)
local remoteEvent = game.ReplicatedStorage:WaitForChild("PromptPurchase")
local monetizationMod = {}

monetizationMod.getTaskOutput = function(player)
	local output = tasksMod.getOutput(player)
	print(output)
	return output
end

local function getStage(stageNum)
	for _, stagePart in pairs(spawnParts:GetChildren()) do
		if stagePart.Stage.Value == stageNum then
			return stagePart
		end
	end
end

monetizationMod.insertTool = function(player, assetId)
	local asset = insertService:LoadAsset(assetId)
	local tool = asset:FindFirstChildOfClass("Tool")
	tool.Parent = player.Backpack
	asset:Destroy()
end

remoteEvent.OnServerEvent:Connect(function(player)
	local allTrue = monetizationMod.getTaskOutput(player)

	if allTrue then
		marketService:PromptPurchase(player, 0000000)
	end
end)

Any assistance or suggestions provided would be highly appreciated. Thank you in advance for your time and help!

If you need further information or have additional questions, please don’t hesitate to ask!

I believe the issue is with this line, you’re putting this globally making it so that if a player has all of this, it sets the value for them and if other players go through this script, the value will be what the previous player was. Try this:

local playerService = game:GetService("Players")
local dataMod = require(script.Parent.Data)
local spawnParts = workspace.SpawnParts
local TasksMod = {}

local function getWins(winsNum)
	for _, winsPart in pairs(spawnParts:GetChildren()) do
		if winsPart.Wins.Value == winsNum then
			return winsPart
		end
	end
end

TasksMod.checkPlayerInGroup = function(player)
	local groupId = 34325147

        return player:IsInGroup(groupId)
end

TasksMod.checkPlayerTime = function(player, TaskGroups)
	local startTime = tick() 

	while true do
		wait(1)
		local currentTime = tick()
		local timeInGame = currentTime - startTime

		if timeInGame >= 20 then
			TaskGroups.checkPlayerTime = true
			break
		end
	end
end

TasksMod.checkPlayerWins = function(player)
	local winsNum = dataMod.get(player, "Wins")
        
        return winsNum == 1
end

local function checkGroups(player)
local TaskGroups = {
	checkPlayerInGroup = false;
	checkPlayerTime = false;
	checkPlayerWins = false;
}

	TaskGroups.checkPlayerInGroup = TasksMod.checkPlayerInGroup(player)
	TaskGroups.checkPlayerWins = TasksMod.checkPlayerWins(player)

	coroutine.wrap(TasksMod.checkPlayerTime)(player, TaskGroups)

	local allTrue = true
	for _, groupValue in pairs(TaskGroups) do
		if groupValue ~= true then
			allTrue = false
			break
		end
	end

	local output = ""
	for groupName, groupValue in pairs(TaskGroups) do
		output = output .. "{" .. groupName .. " = " .. tostring(groupValue) .. "}, "
	end

	output = output .. "{AllTrue = " .. tostring(allTrue) .. "}"

	print(output) 

	return allTrue
end

TasksMod.getOutput = function(player)
	return checkGroups(player)
end

playerService.PlayerAdded:Connect(checkGroups)

return TasksMod

I added a print(test) when the timeInGame >= 20 but it does not update the oultput correctly anymore when i call it from the monetization Module see here:

so the issue is, you created a a coroutine- it will just move passed it and run the rest of the code. You’ll need to use some sort of BindableEvent to fire once the time is greater than 20, or just call the function normally and have the code yield

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.