I’m trying to reference the character in my OOP module script for action functions but it comes as nil in the SetUp function. Everything else prints out correctly except the character reference. Thanks for any help.
local PlayerProfiles = {}
local playerProfile = {}
local profileTable = {}
playerProfile["Name"] = "Loading..."
playerProfile["Ability"] = "Hacker"
playerProfile["UserId"] = nil
playerProfile["Character"] = nil
function PlayerProfiles.new(UserId)
local function deepCopy(original)
local copy = {}
for k, v in pairs(original) do
copy[k] = type(v) == "table" and deepCopy(v) or v
end
return copy
end
local newProfile = deepCopy(playerProfile)
PlayerProfiles[tostring(UserId)] = newProfile
table.insert(profileTable, UserId) -- Store the UserId, not the whole profile
print(profileTable)
return newProfile
end
function PlayerProfiles.getPlayerProfile(plrId)
local stringId = tostring(plrId)
if PlayerProfiles[stringId] then
return PlayerProfiles[stringId]
else
print("profile not found")
return nil -- Return nil if the profile isn't found
end
end
-- Player Specific
function playerProfile:SetUp(plr)
self["Name"] = plr.Name
self["UserId"] = plr.UserId
self["Character"] = plr.Character
print(self["Name"])
print(self["UserId"])
print(self["Character"])
end
--Player Moves
local active = false
function playerProfile:BasicAttack()
print("DidSomething")
print(self["Character"])
local hitboxCFrame = self["Character"]:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, 6)
local hitboxSize = Vector3.new(6, 6, 6)
local createHitbox = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
local HumanoidsHit = {}
for i, v in pairs(createHitbox) do
if v.Parent:FindFirstChild("Humanoid") and not HumanoidsHit[v.Parent.Name] then
HumanoidsHit[v.Parent.Name] = true
v.Parent.Humanoid.Health -= 10
end
end
end
return PlayerProfiles
Server Script
local players = game:GetService("Players")
local playerSetup = require(game.ReplicatedStorage["Player Setup"])
players.PlayerAdded:Connect(function(plr)
playerSetup.new(plr.UserId)
local playersProfile = playerSetup.getPlayerProfile(plr.UserId)
playersProfile:SetUp(plr)
print(playersProfile)
end)