Hello!
I just learned how to use Module scripts and decided to make a module for an existing bit of code I had to check if a player had access to a team and to change it if they did. My goal was to make a module that would check for the ownership of a gamepass or being a privelleged user.
After a couple of hours of trial and error, I managed to make it work. I tried to have the local script change the team with this script:
local function hasAccess()
local hasGamePass = false
local isPrivilegedUser = false
PlayerCheckModule.hasAccess(player, gamePassID, TeamName)
end
TeamChangeButton.MouseButton1Click:Connect(function()
if hasAccess(player, gamePassID) then
player.Team = game.Teams[TeamName]
print(player.Name .. " has changed teams.")
else
warn("player team change denied")
end
end)
But I kept getting errors on
PlayerCheckModule.hasAccess(player, gamePassID, TeamName)
end
like Attempted to call a nil value or Attempted to call a boolean value, so I meshed the team change script into the module script.
What I want to know is,
1. Is it clean enough? Could certain areas be polished, or are there some redundancies I was unaware of?
2. How can I separate the team changing script from the gamepass check script? How do I use a module script to get a boolean value?
MODULE
local PlayerCheckModule = {}
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
--TODO: Make gamepasses
PlayerCheckModule.gamepassTable = { --EXAMPLES FOR NOW
123456789, --Captain
2468101214, -- First Mate
13579111315, -- Premium
}
PlayerCheckModule.privilegedUsernames = {
"hugtrain",
}
PlayerCheckModule.Accepted = false
local function hasAccess(player, gamePassID)
local hasGamePass = false
local isPrivilegedUser = false
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) then
hasGamePass = true
print("success")
else
warn("Gamepass ownership check failed for player: " .. player.Name)
print(hasGamePass)
end
-- Check if the player's username is in the privileged list
isPrivilegedUser = table.find(PlayerCheckModule.privilegedUsernames, player.Name) ~= nil
print(isPrivilegedUser)
print("checked")
return hasGamePass or isPrivilegedUser
end
function PlayerCheckModule.changeTeam(player, gamePassID, TeamName)
print(player, gamePassID, TeamName)
if not player or not player.Character then return end
if hasAccess(player, gamePassID) then
player.Team = game.Teams[TeamName]
print(player.Name .. " has changed teams.")
else
warn("player team change denied")
end
end
return PlayerCheckModule
LOCAL SCRIPT EXAMPLE
local TeamChangeButton = script.Parent -- Reference to the ImageButton instance
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerCheckModule = require(ReplicatedStorage.Modules.PlayerCheckModule)
-- Configuration
local gamePassID = 12345678 -- Replace with Game Pass ID
local TeamName = "Captain" -- Replace with team name
local function hasAccess()
local hasGamePass = false
local isPrivilegedUser = false
print(player, gamePassID, TeamName)
PlayerCheckModule.changeTeam(player, gamePassID, TeamName)
end
TeamChangeButton.MouseButton1Click:Connect(function()
hasAccess()
end)
Thanks for your feedback!