How would I stop a player's body colors from loading?

I’m trying to make a custom forcefield system which changes the player’s body colors while it’s active, however when a player has just loaded their character, sometimes their avatar’s body colors overwrite the color change script. Here is the code:

local function changemat()
		repeat task.wait()
			for i,v in pairs(char:GetDescendants()) do
				if v:IsA("BasePart") then
					if not properties[v] then
						properties[v] = {v.Material,v.Color}
					end
					v.Material = Enum.Material.ForceField
					v.Color = ffcolor
				end
			end until ffvalue.Value == 0
		for i,v in pairs(char:GetDescendants()) do
			if v:IsA("BasePart") and properties[v] ~= nil then
				v.Material = properties[v][1]
				v.Color = properties[v][2]
			end
		end
		local bc = char:FindFirstChildOfClass("BodyColors")
		if bc then
			char.Head.Color = bc.HeadColor3
			char.Torso.Color = bc.TorsoColor3
			char["Right Arm"].Color = bc.RightArmColor3
			char["Left Arm"].Color = bc.LeftArmColor3
			char["Right Leg"].Color = bc.RightLegColor3
			char["Left Arm"].Color = bc.LeftLegColor3
		end
		properties = nil
	end

I have honestly no clue how this is happening considering that I run the color changing code on a loop while the forcefield is active. Any help is appreciated.

Run the function after you use a wait for the signal of the character having loaded completely.

Example:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

	player.CharacterAdded:Connect(function(character)
		
		player.CharacterAppearanceLoaded:Wait()
		--//Run the function
	end)

end)

This should make the function run only after the character has loaded their own appearance, thus making the function always ‘override’ their own character’s colours.

I would do that but sometimes the character doesn’t load in time and I want the forcefield to be visible at all times

In that case, one possibility is to make the character load outside of the play area, wait for a signal that the character is loaded and then move them to the play area, executing the code.
I don’t know if there is a specific way to listen for signals from or disable the loading of the player’s body colors, but you can perhaps utilize the CharacterAutoLoads boolean to your favor.
You can then manually respawn them with the Player:LoadCharacter() function. There is perhaps some area to play with using that method.

Not really an option for me either as I have a game based around 1v1s/duels

Try this:

local function changemat()
    while ffvalue.Value == 1 do 
        task.wait() 
        for i, v in pairs(char:GetDescendants()) do
            if v:IsA("BasePart") then
                if not properties[v] then
                    properties[v] = {v.Material, v.Color}
                end
                v.Material = Enum.Material.ForceField
                v.Color = ffcolor
            end
        end
    end
    
    for i, v in pairs(char:GetDescendants()) do
        if v:IsA("BasePart") and properties[v] ~= nil then
            v.Material = properties[v][1]
            v.Color = properties[v][2]
        end
    end
    
    local bc = char:FindFirstChildOfClass("BodyColors")
    if bc then
        char.Head.Color = bc.HeadColor3
        char.Torso.Color = bc.TorsoColor3
        char["Right Arm"].Color = bc.RightArmColor3
        char["Left Arm"].Color = bc.LeftArmColor3
        char["Right Leg"].Color = bc.RightLegColor3
        char["Left Leg"].Color = bc.LeftLegColor3 -- Fixed typo from "Left Arm" to "Left Leg"
    end
    
    properties = nil -- Clear the properties table
end

Tried it, now the color changing event does not fire at all

1 Like

Nevermind, this seems to have fixed it, thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.