local PlayerController = require(script:WaitForChild("PlayerController"))
local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(Player)
print('1')
PlayerController:Initialize(Player)
print('2')
Player.CharacterAdded:Connect(function(Character)
print('3')
PlayerController:CharacterInitialize(Character)
end)
end)
Players.PlayerRemoving:Connect(function(Player)
PlayerController:Terminate(Player)
end)
1 Like
Modulescript:
local DatastoreController = require(script:WaitForChild("DatastoreController"))
local Controller = script:WaitForChild("Controller")
local PlayerController = {}
function PlayerController:Initialize(Player)
DatastoreController:GetAsync(Player)
end
function PlayerController:CharacterInitialize(Character)
Controller:Clone().Parent = Character
end
function PlayerController:Terminate(Player)
DatastoreController:SetAsync(Player)
end
return PlayerController
Please tell us what you are doing when testing. because I don’t know what your doing when testing your script.
I’m just clicking on “Test” and it prints out only 1 and 2 and CharacterAdded event won’t run
The character might have already loaded by that time. It can depend on how you are testing the game.
Players.PlayerAdded:Connect(function(Player)
print('1')
PlayerController:Initialize(Player)
print('2')
if (Player.Character) then
print('Character already exists')
PlayerController:CharacterInitialize(Character)
end
Player.CharacterAdded:Connect(function(Character)
print('3')
PlayerController:CharacterInitialize(Character)
end)
end)
1 Like
Yup, just figured it out, changed it this to this instead
local PlayerController = require(script:WaitForChild("PlayerController"))
local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(Player)
PlayerController:Initialize(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
PlayerController:CharacterInitialize(Character)
end)
Players.PlayerRemoving:Connect(function(Player)
PlayerController:Terminate(Player)
end)
1 Like