Color doesn't change when pressed button

Hello, I fired a RemoteEvent to a main script in ServerScriptService, which is supposed to make the color of my part different. this is the script:

   game.ReplicatedStorage.MakeRed.OnServerEvent:Connect(function()
    local Player = game.Players.LocalPlayer
    local Backpack = Player.Backpack
    local Tool = Backpack:WaitForChild("LightStick"):WaitForChild("Handle")

Tool.Color = Color3.new(255, 0, 0)
end)

I can’t find the bug in my script.

Can I see the script where you fired the RemoteEvent from?

This is the script:

script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.MakeRed:FireServer()
end)

Are you getting any errors in the output?

Yes, the output says:
ServerScriptService.ColorChangeScript:3: attempt to index nil with ‘Backpack’

One problem was that you were trying to get the tool from the player instead of the player’s character. You would also have to pass the player in from the local script because you can’t get the local player from a server script using Player = game.Players.LocalPlayer. You would have to use the PlayerAdded function to get the local player from a server script.

Server Script:

game.ReplicatedStorage.MakeRed.OnServerEvent:Connect(function(player)
	local Name = player.Name
	local Player = workspace:WaitForChild(Name)
	local Tool = Player:WaitForChild("LightStick"):WaitForChild("Handle")
	Tool.Color = Color3.new(255, 0, 0)
end)

Local Script:

script.Parent.MouseButton1Click:Connect(function()
	player = game.Players.LocalPlayer
	game.ReplicatedStorage.MakeRed:FireServer(player)	
end)
1 Like

you’re supposed to have stuff inside of the :Fireserver event. And in the .Onserverevent for what you sent from the fire server. Also you can’t access player from server. Here’s how i’d do it.
local script:

script.Parent.MouseButton1Click:Connect(function()
	local Player = game.Players.LocalPlayer
	local Backpack = Player.Backpack
	local Tool = Backpack:WaitForChild("LightStick")
	local Handle = Tool:WaitForChild("Handle")
	game.ReplicatedStorage.FixesFolder["MikeGFX's_Folder"].MakeRed:FireServer(Tool, Handle) -- this is what will be sent to the server.
end)

server script:

game.ReplicatedStorage.FixesFolder["MikeGFX's_Folder"].MakeRed.OnServerEvent:Connect(function(Player, Tool, Handle) -- PLAYER IS AUTOMATICLY THERE! DON'T REMOVE PLAYER!
	Handle.BrickColor = BrickColor.new("Really red")
end)

also the reason why its mike gfx folder is because I have a game where I fix people’s errors xD.
Hope this helps.
also if you wanna see how this works then here:
https://gyazo.com/52a23572b579783a31f3ef452af9a385

1 Like

Doesn’t work, but no error catched up
in the output
this is the local script:

script.Parent.MouseButton1Click:Connect(function()
    local Player = game.Players.LocalPlayer
    local Backpack = Player.Backpack
    local Tool = Backpack:WaitForChild("LightStick")
    local Handle = Tool:WaitForChild("Handle")
    game.ReplicatedStorage.FixesFolder["GamepassTools"].MakeRed:FireServer(Tool, Handle) -- this is what will be sent to the server.
end)

this is the script:

game.ReplicatedStorage.FixesFolder["GamepassTools"].MakeRed.OnServerEvent:Connect(function(Player, Tool, Handle) -- PLAYER IS AUTOMATICLY THERE! DON'T REMOVE PLAYER!
    Handle.BrickColor = BrickColor.new("Really red")
end)

Oh, it’s because the tool wasn’t in the backpack. When tools are equipped they’re sent to the player’s character. I’ve gone ahead and fixed it. Here is the new LOCAL SCRIPT, NOT server script. Server script is the same.

script.Parent.MouseButton1Click:Connect(function()
	local Player = game.Players.LocalPlayer
	local Character = Player.Character
	local Backpack = Player.Backpack
	local BackPackTool = Backpack:FindFirstChild("LightStick")
	local CharTool = Character:FindFirstChild("LightStick")
	if BackPackTool then -- if the tool is NOT equipped. Aka in the player's backpack
		BackPackHandle = BackPackTool:FindFirstChild("Handle") -- this is a global variable. If it has an orange line underneath it then don't worry.
	end
	if CharTool then --- if the tool IS equipped. Aka in the player's character.
		CharHandle = CharTool:FindFirstChild("Handle") -- this is a global variable. If it has an orange line underneath it then don't worry.
		end
	if BackPackTool then
		game.ReplicatedStorage.FixesFolder["MikeGFX's_Folder"].MakeRed:FireServer(BackPackTool, BackPackHandle) -- this is what will be sent to the server.
	elseif CharTool then -- if the tool isn't in the back pack then this will happen.
		game.ReplicatedStorage.FixesFolder["MikeGFX's_Folder"].MakeRed:FireServer(CharTool, CharHandle)
		end
end)

if ur wondering how this works then go here: https://gyazo.com/80980a6a947df22eaed88c96a02874b2
If you want one that’s more noticable then go here: https://gyazo.com/300ea3b431bee400631873ad4269ba67

As you can see when I click the button the brick becomes red. Even if it’s equipped or not.

Also if the global variables have orange lines underneath them then dont worry. It won’t break the script.

did the new script work? If it doesn’t work then you can tell me.

Why game.Players.LocalPlayer if it is a server sided script?

I can’t tell if this was mentioned, but a Remote fired from the client always, automatically, sends, Player as the first argument, so if you send one argument; it will be arg #2, by the time you start the Remote on the Server.

Note that the connected function (lines 6-12) will receive the Player who fired the event as its first parameter ( player ), along with any additional parameters passed from the FireServer() call.

output says:
ServerScriptService.ColorChangeScript:2: attempt to index nil with ‘BrickColor’

Please show the server script. And local script. The script works fine with me. Maybe you edited it slightly?

serverscriptservice script:

game.ReplicatedStorage.FixesFolder["GamepassTools"].MakeRed.OnServerEvent:Connect(function(Player, Tool, Handle) -- PLAYER IS AUTOMATICLY THERE! DON'T REMOVE PLAYER!
    Handle.BrickColor = BrickColor.new("Really red")
end)

local script:


script.Parent.MouseButton1Click:Connect(function()
    local Player = game.Players.LocalPlayer
    local Character = Player.Character
    local Backpack = Player.Backpack
    local BackPackTool = Backpack:FindFirstChild("LightStick")
    local CharTool = Character:FindFirstChild("LightStick")
    if BackPackTool then
        BackPackHandle = BackPackTool:FindFirstChild("Handle") -- this is a global variable. If it has an orange line underneath it then don't worry.
    end
    if CharTool then
        CharHandle = CharTool:FindFirstChild("Handle") -- this is a global variable. If it has an orange line underneath it then don't worry.
    end
    if BackPackTool then
        game.ReplicatedStorage.FixesFolder["GamepassTools"].MakeRed:FireServer(BackPackTool, BackPackHandle) -- this is what will be sent to the server.
    elseif CharTool then -- if the tool isn't in the back pack then this will happen.
        game.ReplicatedStorage.FixesFolder["GamepassTools"].MakeRed:FireServer(CharTool, CharHandle)
    end
end)

This is likely because your tool doesn’t have a handle. Can you put a screen shot of your explorer of the tools stuff.

My tool does have a handle, and this is my explorer

OOHHH NVM! I know why! It’s because you did

FixesFolder["Gamepasstools"]

DO

 game.ReplicatedStorage.FixesFolder.GamepassTools.MakeRed

the reason why I did this was because my folder had a speical character in it’s name. An apostrophe. Your folder does NOT have a special character. So do what I said above for all the things that said the other thing.
DO THIS!

 game.ReplicatedStorage.FixesFolder.GamepassTools.MakeRed