#Way I have setup character loading causes conflicts

1 messages · Page 1 of 1 (latest)

meager ore
#

Anyone got any idea on how I could get this to work? My issue is that I need to connect to characteradded so that it loads info on the players character, but I also need to connect to a signal so that the player can change their character by pushing a button. I need this to reset the character so all the new info loads but since characteradded is linked to it it'll load the character twice:

    game.Players.PlayerAdded:Connect(function(player)
        player:GetAttributeChangedSignal("CurrentCharacter"):Once(function()
            self:UpdateCharacter(player, player:GetAttribute("CurrentCharacter"))
        end)
        
        player.CharacterAdded:Connect(function()
            self:UpdateCharacter(player, player:GetAttribute("CurrentCharacter"))
        end)
    end)

    
end

function CharacterService:KnitStart()
    self.Client.UpdateCharacter:Connect(function(player, character)
        player:LoadCharacter()
        self:UpdateCharacter(player, character)
    end)
    
    print(Characters)
end
sinful sable
#

yoo i had this exact issue

#

i figured it out here hold on

#

ill go grab my script and show you what i did

#

local connections = {}

local function characterremoved()
  for i, v in connections do
    v:Disconnect()
    connections[i] = nil
   end
end
    
local function characteradded()
  --Variables to load
  connections.score = score.Changed:Connect(function()
    LeaderstatsClient.Score()
  end)
    
  connections.firelevel = fireLevel.OnClientEvent:Connect(function(var, level)
    LeaderstatsClient.FireLevel(var, level)
  end)
    
  connections.givescore = fireGiveScore.OnClientEvent:Connect(function(score)
    LeaderstatsClient.GiveScore(score)
  end)
end

player.CharacterAdded:Connect(characteradded)

player.CharacterRemoving:Connect(characterremoved)
#

basically make a table with all the connections for every signal and remove them with a characterremoving:Connect function

#

this is direct from my scripts

#

cuz i use a lot of module scripts that cant be 'reloaded' i had to put them into a character added signal

#

ignore all the random arguments they're only useful to my game

meager ore
#

I don't think that'll really help in this case, thanks though!

#

My issue is not connections and stuff

#

It's the fact self:UpdateCharacter() will run twice when the player respawns after selecting a character

sinful sable
#

oh shit true hmm
could you try moving characterAdded outside the playeradded function?

#

because whenever you have nested ;Connect functions it will make the second connect function run twice

#

and characteradded doesnt need to be inside the playeradded function -since it will run as soon as the character gets added anyway

#
    game.Players.PlayerAdded:Connect(function(player)
        player:GetAttributeChangedSignal("CurrentCharacter"):Once(function()
            self:UpdateCharacter(player, player:GetAttribute("CurrentCharacter"))
        end)
    end)

    -- Move outside PlayerAdded to prevent doublefireing
    player.CharacterAdded:Connect(function()
        self:UpdateCharacter(player, player:GetAttribute("CurrentCharacter"))
    end)

    
end

function CharacterService:KnitStart()
    self.Client.UpdateCharacter:Connect(function(player, character)
        player:LoadCharacter()
        self:UpdateCharacter(player, character)
    end)
    
    print(Characters)
end
#

is this on localscript?

meager ore
#

ModuleScript

sinful sable
#

if its not on localscript you might need to add Players:GetPlayerFromCharacter(Char) inside characteradded

#

here

meager ore
#

Oh hold on

#

It seems to work fine now?

sinful sable
#
    game.Players.PlayerAdded:Connect(function(player)
        player:GetAttributeChangedSignal("CurrentCharacter"):Once(function()
            self:UpdateCharacter(player, player:GetAttribute("CurrentCharacter"))
        end)
    end)

    -- Move outside PlayerAdded to prevent doublefireing
    player.CharacterAdded:Connect(function(Char) --add char here
        local player = game:GetService("Players"):GetPlayerFromCharacter(Char) --get player here
        self:UpdateCharacter(player, player:GetAttribute("CurrentCharacter"))
    end)

    
end

function CharacterService:KnitStart()
    self.Client.UpdateCharacter:Connect(function(player, character)
        player:LoadCharacter()
        self:UpdateCharacter(player, character)
    end)
    
    print(Characters)
end
meager ore
#

I appreciate the help dude, thanks!