Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

using events for character customisation?

ive been trying to use events for a few weeks now and i cant seem to crack it. ive been working on this character customisation GUI and it seems that the only thing holding me back is the events and i cant seem to figure out where to start. do i use fireservers or what? , it works by adding values from a > or < button on the gui (if you was wondering) - heres the script

local skin = script.Parent.Value.Value
local player = game.Players.LocalPlayer
while true do 
	wait (0.1)
	if skin == 1 then brickcolor = ("Pastel brown") end
	if skin == 2 then brickcolor = ("Nougat") end
	if skin == 3 then brickcolor = ("Dirt brown") end
	if skin == 4 then brickcolor = ("Tawny") end
	if player:FindFirstChild("Body Colors") then
		local bodyColors = player.Character:WaitForChild("Body Colors")
		bodyColors.Name = "Body Colors"
		
bodyColors.HeadColor = BrickColor.new(brickcolor)
bodyColors.LeftFootColor = BrickColor.new(brickcolor)
bodyColors.LeftHandColor = BrickColor.new(brickcolor)
bodyColors.LeftLowerArmColor = BrickColor.new(brickcolor)
bodyColors.LeftLowerLegCoolor = BrickColor.new(brickcolor)
bodyColors.LeftUpperArmColor = BrickColor.new(brickcolor)
bodyColors.LeftUpperLegColor = BrickColor.new(brickcolor)
bodyColors.LowerTorsoColor = BrickColor.new(brickcolor)
bodyColors.RightFootColor = BrickColor.new(brickcolor)
bodyColors.RightHandColor = BrickColor.new(brickcolor)
bodyColors.RightLowerArmColor = BrickColor.new(brickcolor)
bodyColors.RightLowerLegColor= BrickColor.new(brickcolor)
bodyColors.RightUpperArmColor = BrickColor.new(brickcolor)
bodyColors.UpperTorsoColor = BrickColor.new(brickcolor)
	
end	

end


1 answer

✓ Accepted answer

The first problem with this code is that you use local skin = script.Parent.Value.Value this will store the value in the variable named skin which is never updated so the value will be the same.

The second problem is that you set the colours on the client side meaning the changes will not replicate to other players. You will need to use a remote event if this uses user input to update the skin colour.

Youtube vid on remote events wiki info on remote events

All value object have a Changed event which also passes the new value.

Example:-

script.Parent.Value.Changed:Connect(function(newVal)
	-- update body colours
end)

With your current code it may not run when the player spawns. local scripts under the StarterCharacterScripts will be parented to the playe each time the spawn. The alternative is to use the CharacterAdded event (note both methods are with no remote events and will only be visible to the client).


Using lists

As you have a list of possible brick colours using a table with a array of BrickColor is probably a good option as you can edit it easily.

Example:-

local tbl = {
	BrickColor.new("Pastel brown"),
	BrickColor.new("Nougat"),
	BrickColor.new("Dirt brow"),
	BrickColor.new("Tawny"),
}

script.Parent.Value.Changed:Connect(function(newVal)
	if tbl[newVal] then -- bl[newVal] will return nil if index has no value ie no brickcolor
		print("found", tbl[newVal])
		local newBrickColor = tbl[newVal] -- variable for the brick color
		-- set brick color
	else
		print("index not found")
	end
end)

Final code (with no remote events)

local tbl = {
	BrickColor.new("Pastel brown"), -- value 1
	BrickColor.new("Nougat"), -- value 2
	BrickColor.new("Dirt brow"), -- ect...
	BrickColor.new("Tawny"),
}
-- pls use get service as the names can be changed 
local player = game:GetService("Players").LocalPlayer

local function updateBodyColor(val)
	if tbl[newVal] then return end -- not a valid index

	if player.Character then -- if the player has spawned ie not nil
		local bodyColor = player.Character:WaitForChild("Body Colors")
		local brickColor = tbl[newVal] 
		bodyColors.HeadColor = brickColor 
		-- ect....
	end
end


script.Parent.Value.Changed:Connect(function(newVal)
	updateBodyColor(newVal) -- run the local function with the value
end)

-- if the script does not run when the player spawns
player.CharacterAdded:Connect(function(charModel)
	-- you should give the value object a appropriate name Value.Value is confusing
	updateBodyColor(script.Parent.Value.Value) -- run the same function
end)

I hope this helps. If you want this to be visible to all players you should look at how remote events work and move the updateBodyColor function to a server script so that the server can update the bpdy colors.

This discussion is preserved as it was. New answers, comments, and votes are closed.