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?
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)
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)