I’m trying to change a brick’s color on the server side when the player clicks on a brick/part.
When I do click I get this error: Color is not a valid member of Player "Players.Player1"
When I print the player and partInstance I don’t get an instance when I click a part.
Here is my code:
(ServerScriptService)
local RepStorage = game:GetService("ReplicatedStorage")
local rayCastedEvent = RepStorage:WaitForChild("rayCasted")
function partCastedManager(partInstance, player)
partInstance.Color = Color3.new(0.239216, 0.505882, 1)
print(player)
end
rayCastedEvent.OnServerEvent:Connect(partCastedManager)
(starterCharacterScripts)
local uis = game:GetService("UserInputService")
local runServ = game:GetService("RunService")
local cam = workspace.CurrentCamera
local item = nil
local rayCastedEvent = game:GetService("ReplicatedStorage"):WaitForChild("rayCasted")
local function CreateRayCast()
local mousePosition = uis:GetMouseLocation()
local rayCast = cam:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local rcResult = workspace:Raycast(rayCast.Origin, rayCast.Direction * 200)
return rcResult
end
uis.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local result = CreateRayCast()
if result and result.Instance then
--print(result.Instance.Name)
--result.Instance.Color = Color3.new(1, 0.054902, 0.670588)
rayCastedEvent:FireServer(result.Instance, game.Players.LocalPlayer)
end
end
end)
--runServ.RenderStepped:Connect(function()
-- local result = CreateRayCast()
-- if result and result.Instance then
-- print(result.Instance.Name)
-- end
--end)
I do not know what the issue is, all that I can guess is that my code is using the player param as the part instance param.
thats a common mistake, you need to know that when a server recieves an event from the client, it automatically sets the first param as the player/client who triggered the event.
so in ur server script, the first param is passed is a player, the second will be the instance(from client), third will be tha player again(from client)
jusr replace it with:
function partCastedManager(player, partInstance)
partInstance.Color = Color3.new(0.239216, 0.505882, 1)
print(player)
end
Currently I’m not home so I’m unable to test your solution but to get it right, this is what I should replace my current code? (I’d also be changing the function too)
Do not flip the parameters. The first parameter from a OnServerEvent is always an implicit Player instance, which comes from the player who called it. You do not have to explicitly pass it down the parameters for the RemoteFunction because it is implicit and automatic. Therefore, the changes:
local RepStorage = game:GetService("ReplicatedStorage")
local rayCastedEvent = RepStorage:WaitForChild("rayCasted")
function partCastedManager(player, partInstance)
partInstance.Color = Color3.new(0.239216, 0.505882, 1)
print(player)
end
rayCastedEvent.OnServerEvent:Connect(partCastedManager)
local uis = game:GetService("UserInputService")
local runServ = game:GetService("RunService")
local cam = workspace.CurrentCamera
local item = nil
local rayCastedEvent = game:GetService("ReplicatedStorage"):WaitForChild("rayCasted")
local function CreateRayCast()
local mousePosition = uis:GetMouseLocation()
local rayCast = cam:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local rcResult = workspace:Raycast(rayCast.Origin, rayCast.Direction * 200)
return rcResult
end
uis.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local result = CreateRayCast()
if result and result.Instance then
--print(result.Instance.Name)
--result.Instance.Color = Color3.new(1, 0.054902, 0.670588)
rayCastedEvent:FireServer(result.Instance)
end
end
end)
--runServ.RenderStepped:Connect(function()
-- local result = CreateRayCast()
-- if result and result.Instance then
-- print(result.Instance.Name)
-- end
--end)