What is wrong with my script

-- LocalScript / StarterGui
local RS = game:GetService("ReplicatedStorage")
local colorEvent = RS:WaitForChild("Color")

local colorCode = {
	["Red"] = Color3.new(255, 0, 0);
}

script.Parent.Red.MouseButton1Click:Connect(function()
	colorEvent:FireServer(colorCode.Red)
	print(colorCode.Red)
end)
-- Script / ServerScriptService
local RS = game:GetService("ReplicatedStorage")
local colorEvent = RS:WaitForChild("Color")
local part = RS:WaitForChild("PartTS"):FindFirstChild("NormalPart")

colorEvent.OnServerEvent:Connect(function(plr, colorCode)
	print("Fired")
	print(colorCode)
	part.Color = Color3.new(colorCode)
	print("Normal Part color Changed to",part.Color)
end)
-- Output
* 'print(colorCode.Red)': 255, 0, 0 <- Local Script
* 'print("Fired")': Fired <- Script
* 'print("colorCode)': 255, 0, 0 <- Script
* 'print("Normal Part color Changed to",part.Color)': 0, 0, 0 <- Here`s the problem

As I explained above, the problem is with the color, I don’t know why my NormalPart doesn’t change color, even though after detecting it from two sides LocalScript and Script, the color has the same code, but when I move it to NormalPart, the color becomes 0, 0 , 0 or black, maybe I’m using the FireServer function incorrectly. Please Help

2 Likes

I think the problem is because you’re using Color3.new instead of Color3.fromRGB, so try changing the first script to this:

-- LocalScript / StarterGui
local RS = game:GetService("ReplicatedStorage")
local colorEvent = RS:WaitForChild("Color")

local colorCode = {
	["Red"] = Color3.fromRGB(255, 0, 0);
}

script.Parent.Red.MouseButton1Click:Connect(function()
	colorEvent:FireServer(colorCode.Red)
	print(colorCode.Red)
end)

Color3.new only accepts values between 1 and 0 (where 1 is equivalent to 255) while fromRGB accepts 0 to 255
Edit: You also need to change the server script like so:

-- Script / ServerScriptService
local RS = game:GetService("ReplicatedStorage")
local colorEvent = RS:WaitForChild("Color")
local part = RS:WaitForChild("PartTS"):FindFirstChild("NormalPart")

colorEvent.OnServerEvent:Connect(function(plr, colorCode)
	print("Fired")
	print(colorCode)
	part.Color = colorCode
	print("Normal Part color Changed to",part.Color)
end)
3 Likes

image
This still doesn’t work, the color code that was 255, 0, 0 becomes 1, 0, 0 and the final result is 0, 0, 0

Edit: Wait the result is 1, 0, 0 but why its 1, 0, 0 not 255

I’m going to write you new scripts so please wait for a bit while I do

1 Like

Really? never noticed that when passing a Color from client to server, it becomes a 1,1,1 on server side…
Well why dont you change the approach, and the color table should be on the server script, and the only thing that client send thru the remote is the name of the color?

I want to get a specific color, not just a color name, but for the color table in the server side, let me try

… I mean, you have the table in server, just use that name as key to find the right color in the table

LocalScript:

local colorEvent = game:GetService"ReplicatedStorage".Color

local colorCode = {
	Red = Color3.fromRGB(255, 0, 0)
}

script.Parent.Red.MouseButton1Click:Connect(function()
	colorEvent:FireServer(colorCode.Red)
	print(colorCode.Red)
end)

Server Script:

local replicatedStorage = game:GetService"ReplicatedStorage"

local colorCode = {
	Red = Color3.fromRGB(255, 0, 0)
}

local part = replicatedStorage.PartTS.NormalPart

replicatedStorage.Color.OnServerEvent(function(_, color)
	if typeof(color) == "string" then -- For security
		if colorCode[color] then
			print("Fired with color "..color)
			part.Color = colorCode[color]
			print("Normal part color changed to "..color)
		else
			warn(color.." is not found in server colorCode list")
		end
	end
end)
2 Likes

Exactly what I meant, cmon @ryanhawari17 , that should work, give him the right answer

1 Like

Alright wait, let me try that

[LIMIT][LIMIT]

Hold on, this table became useless. The client should send only the string

1 Like

Ohh It works! thanks didn’t think of using it that way before

2 Likes

You’re right actually, this also works correctly (LocalScript):

local colorEvent = game:GetService"ReplicatedStorage".Color

script.Parent.Red.MouseButton1Click:Connect(function()
	colorEvent:FireServer"Red"
	print(colorCode.Red)
end)
2 Likes

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