ClientScript is a localscript inside StarterCharacterScripts. PlayerInit is a serverscript inside ServerScriptService. When I initially load in, everything prints as expected but when I respawn after dying once, it only prints ‘initializing guis’. I have no idea how to fix this or what is producing the issue. Any help?
PlayerInit
local Players = game:GetService('Players')
local Modules = game:GetService('ReplicatedStorage').Modules
local Characters = game:GetService('ReplicatedStorage').Characters
local Events = game:GetService('ReplicatedStorage').Events
local InitializeBar = Events.InitializeBar
local SetupMoves = Events.SetupMoves
local CharactersModule = require(Characters.CharactersModule)
local Default_Character = 'Itadori'
Players.PlayerAdded:Connect(function(Player)
local ChosenCharacter = Instance.new("StringValue", Player)
ChosenCharacter.Value = Default_Character
ChosenCharacter.Name = 'ChosenCharacter'
Player.CharacterAdded:Connect(function(Character)
local CharacterValue = Instance.new("StringValue", Character)
CharacterValue.Name = 'CharacterValue'
CharacterValue.Value = ChosenCharacter.Value
if CharacterValue then
for i, v in CharactersModule.Characters do
if i == CharacterValue.Value then
InitializeBar:FireClient(Player, v.AwakeningTheme)
SetupMoves:FireClient(Player, v.Base)
print('initializing guis')
end
end
end
end)
end)
ClientScript
local Players = game:GetService('Players')
local Events = game:GetService('ReplicatedStorage'):WaitForChild('Events')
local InitializeBar = Events.InitializeBar
local SetupMoves = Events.SetupMoves
local Modules = game:GetService('ReplicatedStorage'):WaitForChild('Modules')
local MovesGuiHandler = require(Modules.MovesGuiHandler)
SetupMoves.OnClientEvent:Connect(function(Base)
print('SetupMoves')
for i, v in Base do
MovesGuiHandler.createMove(v)
end
end)
InitializeBar.OnClientEvent:Connect(function(awakeningTheme)
print('InitializeBar')
local Colors = awakeningTheme.Colors
MovesGuiHandler.applyTheme(Color3.fromRGB(Colors.R,Colors.G,Colors.B), awakeningTheme.Text)
end)
Because your only doing it on the character that was loaded in when you first joined, when the character dies there no event attached to the new character thus why it’s not working
You can easily do this by instead of doing it only for the player you can use the character.
Playerinit remains intact (unchanged)
The only issue is how your clientscript fires it.
Paste this in the clientscript
local Players = game:GetService('Players')
local Events = game:GetService('ReplicatedStorage'):WaitForChild('Events')
local InitializeBar = Events.InitializeBar
local SetupMoves = Events.SetupMoves
local Modules = game:GetService('ReplicatedStorage'):WaitForChild('Modules')
local MovesGuiHandler = require(Modules.MovesGuiHandler)
local function setupEventHandlers()
print('Setting up event handlers')
SetupMoves.OnClientEvent:Connect(function(Base)
print('SetupMoves')
for i, v in Base do
MovesGuiHandler.createMove(v)
end
end)
InitializeBar.OnClientEvent:Connect(function(awakeningTheme)
print('InitializeBar')
local Colors = awakeningTheme.Colors
MovesGuiHandler.applyTheme(Color3.fromRGB(Colors.R, Colors.G, Colors.B), awakeningTheme.Text)
end)
end
setupEventHandlers()
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
setupEventHandlers()
end)
end)
I’m assuming his localscript that fires this is the clientscript [as he stated] that’s what should fire it. and the playerinit is the one that handles everything and responds to the client.
This addition simply makes it so that it: checks on player added, uses the player to get their character.
and lastly fires the setupeventhandlers function.
Now why this works is cause once the player is added and the character is added. It will would keep looping this event ‘CharacterAdded’ each time they reset/respawn.
workspace.ChildAdded:Connect(function(child)
if game.Players:FindFirstChild(child.Name) then
local player = game.Players:FindFirstChild(child.Name)
local CharacterValue = Instance.new("StringValue", child)
CharacterValue.Name = 'CharacterValue'
CharacterValue.Value = player.ChosenCharacter.Value
if CharacterValue then
for i, v in CharactersModule.Characters do
if i == CharacterValue.Value then
InitializeBar:FireClient(player, v.AwakeningTheme)
SetupMoves:FireClient(player, v.Base)
print('initializing guis')
end
end
end
end
end)
Players.PlayerAdded:Connect(function(Player)
local ChosenCharacter = Instance.new("StringValue", Player)
ChosenCharacter.Value = Default_Character
ChosenCharacter.Name = 'ChosenCharacter'
end)
Simply put this in the playerinit script. this should work i assume
local Players = game:GetService('Players')
local Modules = game:GetService('ReplicatedStorage').Modules
local Characters = game:GetService('ReplicatedStorage').Characters
local Events = game:GetService('ReplicatedStorage').Events
local InitializeBar = Events.InitializeBar
local SetupMoves = Events.SetupMoves
local CharactersModule = require(Characters.CharactersModule)
local Default_Character = 'Itadori'
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
local ChosenCharacter = Instance.new("StringValue", Player)
ChosenCharacter.Value = Default_Character
ChosenCharacter.Name = 'ChosenCharacter'
Player.CharacterAdded:Connect(function(Character)
local CharacterValue = Instance.new("StringValue", Character)
CharacterValue.Name = 'CharacterValue'
CharacterValue.Value = ChosenCharacter.Value
if CharacterValue then
for i, v in CharactersModule.Characters do
if i == CharacterValue.Value then
InitializeBar:FireClient(Player, v.AwakeningTheme)
SetupMoves:FireClient(Player, v.Base)
print('initializing guis')
end
end
end
end)
end)
end)
Then, after the waitforchild, add a task.wait(0.1) or just send a signal to the server that he is loaded (with a remote event) and do for example Loaded.OnServerEvent:Wait()
Well think about it, the childadded event will always fire regardless, so why dont we piggback off that thing and check if a character has been loaded into workspace and have it do our DIRTY WORK FOR US