Remote event not working

I was trying to make a tool to change parts state. I have this local script in a tool:

local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local players = game.Players
local player = players.LocalPlayer

local nodes = workspace.Nodes

local mouse = player:GetMouse()


local function ChangeState(actionName, inputObject, inputState)
	
	local target = mouse.Target
	local occupancy
	local material
	local color
	local transparency
	
	print(target)
	
	if inputObject == Enum.UserInputType.MouseButton1 and inputState == Enum.UserInputState.Begin and nodes:IsAncestorOf(target) then
		if target:GetAttribute("Occupied") == true then
			occupancy = false
			material = Enum.Material.Plastic
			color = Color3.fromRGB(163, 162, 165)
			transparency = 0
		else
			occupancy = true
			material = Enum.Material.Brick
			color = Color3.fromRGB(117, 0, 0)
			transparency = 1
		end
		ReplicatedStorage.Events.ChangeState:FireServer(target, occupancy, material, color, transparency)
	end
end

mouse.Move:Connect(ChangeState)

ContextActionService:BindAction("ChangeState", ChangeState, false, Enum.UserInputType.MouseButton1)

And this regular script in server script service:

game.ReplicatedStorage.Events.ChangeState.OnServerEvent:Connect(function(player, target, occupancy, material, color, transparency)
	local part = target
	part.Material = material
	part.Color = color
	part.Decal.Transparency = transparency
end)

The local script seems to work, because it prints out a target, but the remote event seems not to work, as the part clearly does not change. What could be the problem?

1 Like

Hi! Main problem is you mixed up inputstate and inputobject.
Also i did some improvements:
Removed useless (in this case) variables and added :GetService()

local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")
local player = players.LocalPlayer

local nodes = workspace.Nodes

local mouse = player:GetMouse()


local function ChangeState(actionName, inputState)

	local target = mouse.Target
	local material
	local color
	local transparency

	print(target)
	
	if inputState == Enum.UserInputState.Begin and nodes:IsAncestorOf(target) then
		if target:GetAttribute("Occupied") then
			material = Enum.Material.Plastic
			color = Color3.fromRGB(163, 162, 165)
			transparency = 0
		else
			material = Enum.Material.Brick
			color = Color3.fromRGB(117, 0, 0)
			transparency = 1
		end
		ReplicatedStorage.Events.ChangeState:FireServer(target, material, color, transparency)
	end
end

ContextActionService:BindAction("ChangeState", ChangeState, false, Enum.UserInputType.MouseButton1)
game:GetService("ReplicatedStorage").Events.ChangeState.OnServerEvent:Connect(function(player, target, material, color, transparency)
	target.Material = material
	target.Color = color
	target.Transparency = transparency
end)
1 Like

MouseButton1 is not an input object?

MouseButton1 is a InputObject.UserInputType.
But you don’t need to check it because the only function calling ChangeState() is binded to MouseButton1

Ok, I got it, I will try your variant and see if that works.

It does work on brick, but not on plastic for some reason. Ill dig further into that

Check the Occupied attribute. Maybe you set it to true somewhere in scripts?

You forgot about occupancy variable. It was not useless, as it was one of the main variables to be checked and changed, but overall it works.

Ok, but you can just send target:GetAttribute("Occupied") to server instead creating new variable for this

The problem is, I need to get the opposite of what I get. I check part, it has occupied = true, then I need to set it to false.

In this case not operator.

I recommend learn the operators well because it very useful for huge amount of purposes

Yeah, that works too, I guess.

I’ve done some changes to your code. Now it works completely fine. The reason for the tool not working on plastic, was that the part was not in the nodes folder.
Local:

local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")
local player = players.LocalPlayer

local nodes = workspace.Nodes

local mouse = player:GetMouse()


local function ChangeState(actionName, inputState)

	local target = mouse.Target
	local occupancy
	local material
	local color
	local transparency

	print(target)

	if inputState == Enum.UserInputState.Begin and nodes:IsAncestorOf(target) then
		if target:GetAttribute("Occupied") == true  then
			occupancy = false
			material = Enum.Material.Plastic
			color = Color3.fromRGB(163, 162, 165)
			transparency = 0
		elseif target:GetAttribute("Occupied") == false then
			occupancy = true
			material = Enum.Material.Brick
			color = Color3.fromRGB(117, 0, 0)
			transparency = 1
		end
		ReplicatedStorage.Events.ChangeState:FireServer(target, occupancy, material, color, transparency)
	end
end

ContextActionService:BindAction("ChangeState", ChangeState, false, Enum.UserInputType.MouseButton1)

Server:

game:GetService("ReplicatedStorage").Events.ChangeState.OnServerEvent:Connect(function(player, target, occupancy, material, color, transparency)
	target.Material = material
	target.Color = color
	target.Decal.Transparency = transparency
	target:SetAttribute("Occupied", occupancy)
end)

Thanks again for your help!

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