I have a color wheel that I want to change the color value of a hair. I have two scripts, local, and server. the serverscript has an error, and i dunno how to fix. (error: Line 5, attempt to concatenate string with instance)
Server script
local Remote = game:GetService("ReplicatedStorage").ColorChange
Remote.OnServerEvent:Connect(function(Player, ActionRemote, BackgroundColor3)
local dummy = game.Workspace.customizegui.l
local hair = game:GetService("ReplicatedStorage").Hair["Hair" .. BackgroundColor3] -- error is here btw
if ActionRemote == "ChangeColor" then
if hair.BrickColor then
hair.BrickColor:Destroy()
end
local x = game:GetService("StarterGui").CustomizeGUI.ColourWheelGui.ColourDisplay.BackgroundColor3
x.Parent = hair
end
end)
Local Script:
local Remote = game:GetService("ReplicatedStorage").ColorChange
local player = game.Players.LocalPlayer
local PGUI = player:WaitForChild("PlayerGui")
local ColourWheelGui = game:GetService("StarterGui").CustomizeGUI.ColourWheelGui
local RS = game:GetService("ReplicatedStorage")
local mouse = player:GetMouse()
script.Parent.Parent.CustomizeGUI.ColourWheelGui.ColourWheel.MouseButton1Down:Connect(function()
Remote:FireServer("ChangeColor", mouse.Target,ColourWheelGui.ColourDisplay.BackgroundColor3)
end)
It looks like the error is occurring because you are trying to concatenate a string and an instance ( "Hair" .. BackgroundColor3 ). In Roblox, you can only concatenate strings with other strings, so you’ll need to convert the instance to a string before you can concatenate it.
One way to do this is to use the tostring() function, which will convert the instance to a string. You can then use that string to access the Hair object in game:GetService("ReplicatedStorage") .
local Remote = game:GetService("ReplicatedStorage").ColorChange
Remote.OnServerEvent:Connect(function(Player, ActionRemote, BackgroundColor3)
local dummy = game.Workspace.customizegui.l
local hair = game:GetService("ReplicatedStorage").Hair["Hair" .. tostring(BackgroundColor3)]
if ActionRemote == "ChangeColor" then
if hair.BrickColor then
hair.BrickColor:Destroy()
end
local x = game:GetService("StarterGui").CustomizeGUI.ColourWheelGui.ColourDisplay.BackgroundColor3
x.Parent = hair
end
end)
Got a new error that says “HairTerrain is not a valid member of folder ReplicatedStorage.Hair”
The error you are seeing is caused by attempting to concatenate a string with an instance. In this case, the error is occurring on the following line of code:
local hair = game:GetService("ReplicatedStorage").Hair["Hair" .. BackgroundColor3]
In this line, you are trying to access a property of the Hair object in the ReplicatedStorage service by concatenating the string “Hair” with the BackgroundColor3 value. However, BackgroundColor3 is an instance of the Color3 class, not a string, so you cannot concatenate it with a string.
To fix this error, you can convert the BackgroundColor3 value to a string using the tostring function, and then concatenate it with the “Hair” string to access the correct property of the Hair object. Here is an example of how you could modify your code to do this:
local hair = game:GetService("ReplicatedStorage").Hair["Hair" .. tostring(BackgroundColor3)]
With this change, the concatenation operation will produce a valid string that can be used to access the correct property of the Hair object. This should fix the error and allow your code to run correctly.
lke this? i got an error saying infinite yield possible. THere are 6 different hairs inside of the hair folder. Each with a name like Hair1, Hair2, etc. until Hair6
I’m not quite sure how to wait for all of those children.
WaitForChild doesn’t accept multiple instance parameters.
Instead, try using multiple variables for them.
Example:
local rs = game:GetService("ReplicatedStorage")
local hair1 = rs:WaitForChild("Hair1" .. tostring(BackgroundColor3), 1) -- The 1 defines a timeout for WaitForChild, so if the hair does not exist, we don't spend forever trying to get it.
local hair2 = rs:WaitForChild("Hair2" .. tostring(BackgroundColor3), 1)
local hair3 = rs:WaitForChild("Hair3" .. tostring(BackgroundColor3), 1)
-- etc...
Then, when getting the hair,
if hair1 then
-- insert code for hair1 here
end
if hair2 then
-- insert code for hair2 here
end
-- and so on...
local Remote = game:GetService("ReplicatedStorage").ColorChange
local rs = game:GetService("ReplicatedStorage").Hair
Remote.OnServerEvent:Connect(function(Player, ActionRemote, BackgroundColor3)
local hair1 = rs:WaitForChild("Hair1" .. tostring(BackgroundColor3), 1)
local hair2 = rs:WaitForChild("Hair2" .. tostring(BackgroundColor3), 1)
local hair3 = rs:WaitForChild("Hair3" .. tostring(BackgroundColor3), 1)
local hair4 = rs:WaitForChild("Hair4" .. tostring(BackgroundColor3), 1)
local hair5 = rs:WaitForChild("Hair5" .. tostring(BackgroundColor3), 1)
local hair6 = rs:WaitForChild("Hair6" .. tostring(BackgroundColor3), 1)
local c = game.StarterGui.CustomizeGUI.ColourWheelGui.ColourDisplay.BackgroundColor3
local dummy = game.Workspace.customizegui.l
local char = game.Players.LocalPlayer
if ActionRemote == "ChangeColor" then
if hair1.Parent == dummy or hair1.Parent == char then
hair1.BrickColor = c
end
if hair2.Parent == dummy or hair2.Parent == char then
hair2.BrickColor = c
end
if hair3.Parent == dummy or hair3.Parent == char then
hair3.BrickColor = c
end
if hair4.Parent == dummy or hair4.Parent == char then
hair4.BrickColor = c
end
if hair5.Parent == dummy or hair5.Parent == char then
hair5.BrickColor = c
end
if hair6.Parent == dummy or hair6.Parent == char then
hair6.BrickColor = c
end
local x = game:GetService("StarterGui").CustomizeGUI.ColourWheelGui.ColourDisplay.BackgroundColor3
x.Parent = hair1
x.Parent = hair2
x.Parent = hair3
x.Parent = hair4
x.Parent = hair5
x.Parent = hair6
end
end)
No, before checking hair1.Parent, first check if Hair1 exists
if hair1 and (hair1.Parent == dummy or hair1.Parent == char) then
hair1.BrickColor = c
end
if hair2 and (hair2.Parent == dummy or hair2.Parent == char) then
hair2.BrickColor = c
end
-- and so on
I made a few changes, and now I get no errors, yet the hair color doesn’t change (I removed the hairs down to 3 hairs, this doesnt change the script functionality…
local Remote = game:GetService("ReplicatedStorage").HairColorChange
local rs = game:GetService("ReplicatedStorage").Hair
Remote.OnServerEvent:Connect(function(Player, ActionRemote, BackgroundColor3)
local hair1 = rs:WaitForChild("Hair1" .. tostring(BackgroundColor3), 1)
local hair2 = rs:WaitForChild("Hair2" .. tostring(BackgroundColor3), 1)
local hair3 = rs:WaitForChild("Hair3" .. tostring(BackgroundColor3), 1)
local c = game.StarterGui.CustomizeGUI.ColourWheelGui.ColourDisplay.BackgroundColor3
local dummy = game.Workspace.customizegui.l
local char = game.Players.LocalPlayer
if ActionRemote == "ChangeColor" then
if hair1 and (hair1.Parent == dummy or hair1.Parent == char) then
hair1.BrickColor = c
end
if hair2 and (hair2.Parent == dummy or hair2.Parent == char) then
hair2.BrickColor = c
end
if hair3 and (hair3.Parent == dummy or hair3.Parent == char) then
hair3.BrickColor = c
end
local x = game:GetService("StarterGui").CustomizeGUI.ColourWheelGui.ColourDisplay.BackgroundColor3
game.ReplicatedStorage.Hair.Hair1.Handle.Color = x
game.ReplicatedStorage.Hair.Hair2.Handle.Color = x
game.ReplicatedStorage.Hair.Hair3.Handle.Color = x
end
end)
Also this may help, but here is my replicated storage…
local Remote = game:GetService("ReplicatedStorage").HairColorChange
local rs = game:GetService("ReplicatedStorage").Hair
Remote.OnServerEvent:Connect(function(Player, ActionRemote, BackgroundColor3)
local hair1 = char:WaitForChild("Hair1", 1)
local hair2 = char:WaitForChild("Hair2", 1)
local hair3 = char:WaitForChild("Hair3", 1)
if ActionRemote == "ChangeColor" then
local char = Player.Character
local dummy = workspace.customizegui.l
if hair1 and (hair1.Parent == dummy or hair1.Parent == char) then
hair1.Color = BackgroundColor3
end
if hair2 and (hair2.Parent == dummy or hair2.Parent == char) then
hair2.Color = BackgroundColor3
end
if hair3 and (hair3.Parent == dummy or hair3.Parent == char) then
hair3.Color = BackgroundColor3
end
end
end)
There were a few problems, the most notable being you tried to reference Players.LocalPlayer on the server, and you tried to reference StarterGUI to get the colors of the hairs.
Hope this works!