Matching a tools color to the players right arm color

Now, i am fairly new to scripting having only started 2 or 3 months ago but I have been trying so hard to get this to work and its not working, as you can tell from the title im trying to get a tool to match the players right arm skin color.

I have tried many solutions like changing it from a local script to a script, changing the properties of the handle and many more including stuff in the script itself.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end

game.Workspace.ChildAdded:Connect(function()
    player.CharacterAdded:Wait()
	local color = script.Parent.BrickColor
	local tool = script.Parent.Parent
	local player = game.Players.LocalPlayer
	local character = player.Character
	local bodycolors = character.BodyColors

tool.Equipped:Connect(function()
	script.Parent.BrickColor = BrickColor.new(bodycolors.RightArmColor)
   end)
end)

image

Players.LocalPlayer does not return anything on a server-side script. Because LocalPlayer is meant to return the player that belongs to the individual machine the code is running on, what is it supposed to return if that machine is the server itself?

The ChildAdded code doesn’t close itself properly, is redundant, and listening for ChildAdded in workspace shouldn’t be what you’re looking for in a script meant to change a tool’s color to the arm color of whoever’s using it.

Anyways, here’s come working code. This should be ran locally.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- works the same as the if statement
if not player:HasAppearanceLoaded() then
	player.CharacterAppearanceLoaded:Wait() -- this includes the BodyColors object
end
local bodycolors = character["Body Colors"] -- BodyColors object of a normal character has a space in the name
-- tool and handle probably should be set as variables
local tool = script.Parent.Parent
local handle = script.Parent

tool.Equipped:Connect(function()
	handle.BrickColor = bodycolors.RightArmColor -- using BrickColor.new on a BrickColor value is redundant
end)

I apologize if I’m being overbearing.

2 Likes

Don’t worry you weren’t overbearing, you taught me alot. Thank you so much!

1 Like

Okay so, i am so sorry for responding like this but i have been trying to get it to work on the server side with a remote event and i was wondering if you could help me?

local rs = game:GetService("ReplicatedStorage")
local skinchange = rs.skinchange
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local chr = player.Character or player.CharacterAdded:Wait()
	if not player:HasAppearanceLoaded() then
		player.CharacterAppearanceLoaded:Wait()
	end

		
		local bodycolors = chr["Body Colors"]
		skinchange.OnServerEvent:Connect(function(player, handle)
			handle.BrickColor = bodycolors.RightArmColor
		end)
end)

Ok so above me is the server script i put into serverscriptservice

and under me is the one the one thats in the tool (its obv a local script)

local rs = game:GetService("ReplicatedStorage")

local skinchange = rs.skinchange

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

if not player:HasAppearanceLoaded() then

player.CharacterAppearanceLoaded:Wait()

end

local bodycolors = character["Body Colors"]

local tool = script.Parent.Parent

local handle = script.Parent

tool.Equipped:Connect(function()

skinchange:FireServer(handle)

end)

Again i am so sorry for replying like this but i have been trying so hard its just not working, the problem is that it sometimes works and sometimes it just doesnt, also when i reset it just stops working, if you are able to, Help would be appreciated! :smiley:

The code stopped working when you reset because the code attempted to work off a reference to a BodyColors object that no longer existed. A variable is a static reference to a specific thing. When you respawn, your character is destroyed and a new one is created, but the bodycolors variable will reference the same, nonexistent object unless you change what the variable references.

The PlayerAdded code is unneeded. When the remote fires to the server, you’ll be getting the corresponding Player as an argument of OnServerEvent anyways. The logic of relying on PlayerAdded in the way you do would also mean that the code only finds the character of a player specifically when they spawn in for the first time (they may die a few times while visiting), and that everyone’s Handle color would unnecessarily update whenever anyone equipped the same tool.

You don’t need to wait for Character, CharacterAdded, or CharacterAppearance on the server either, as the local script already does that.

So, the server script should be like this.

local rs = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local skinchange = rs.skinchange

skinchange.OnServerEvent:Connect(function(player, handle)
	local chr = player.Character
	local bodycolors = chr["Body Colors"]
	
	handle.BrickColor = bodycolors.RightArmColor
end)
1 Like

Oh, i don’t know how i didn’t realize that. Anyways i tried getting other solutions for this but yours is the only one that worked, thank you so much for being patient with me and helping me, you are a really good person. :smiley:

1 Like
local Script = script
local Tool = Script.Parent
local Handle = Tool.Handle

local function OnEquipped()
	local Character = Tool.Parent
	if not Character then return end
	local BodyColors = Character:FindFirstChildOfClass("BodyColors")
	if not BodyColors then return end
	Handle.BrickColor = BodyColors.RightArmColor
end

Tool.Equipped:Connect(OnEquipped)

No need to use a ‘RemoteEvent’ object for this.

2 Likes