Others can't see movement from script

This script is meant to move a certain part, but it isn’t server sided.

local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight1.Arm
local UpdateCon

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.A then return end

	if UpdateCon then
		UpdateCon:Disconnect()
		UpdateCon = nil
	end

	UpdateCon = RunService.Heartbeat:Connect(function(dt)
		local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(0, -0.05, 0)
		SpotlightHead:SetPrimaryPartCFrame(newCFrame)
	end)
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.A then return end
	if not UpdateCon then return end

	UpdateCon:Disconnect()
	UpdateCon = nil
end)

Is there any way to implement RemoteEvents into this? (or make it so others can see movement in general)

use normal script
–characters–

Create an event inside of ReplicatedStorage and fire it whenever you start or stop holding the button. When the server receives the event it will toggle a value that’ll enable turning.

--// SERVER (SERVERSCRIPTSERVICE) //--
local REP = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")

local spotlightEvent = REP.SpotlightEvent

local SpotlightHead = game.Workspace.Spotlight1.Arm
local spotlightTurning

RS.Heartbeat:Connect(function()
	if spotlightTurning then
		local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(0, -0.05, 0)
		SpotlightHead:SetPrimaryPartCFrame(newCFrame)
	end
end)

spotlightEvent.OnServerEvent:Connect(function(plr, state)
	spotlightTurning = state
end)
--// CLIENT (STARTERPLAYERSCRIPTS) //--
local REP = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local spotlightEvent = REP.SpotlightEvent

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end --// Prevent his from being executed when player is typing in chat.
	if input.KeyCode == Enum.KeyCode.A then
		spotlightEvent:FireServer(true)
	end
end)

UIS.InputEnded:Connect(function(input, gp)
	if gp then return end --// Prevent his from being executed when player is typing in chat.
	if input.KeyCode == Enum.KeyCode.A then
		spotlightEvent:FireServer(false)
	end
end)

Hope this helps! :slight_smile:

I fixed up parts to go to what I need (controlling a RobotArm’s specific parts)

--// CLIENT (STARTERPLAYERSCRIPTS) //--
local REP = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local remote = REP.FK1

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end --// Prevent his from being executed when player is typing in chat.
	if input.KeyCode == Enum.KeyCode.G then
		remote:FireServer(true)
	end
end)

UIS.InputEnded:Connect(function(input, gp)
	if gp then return end --// Prevent his from being executed when player is typing in chat.
	if input.KeyCode == Enum.KeyCode.G then
		remote:FireServer(false)
	end
end)
--// SERVER (SERVERSCRIPTSERVICE) //--
local REP = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")

local remote = REP.FK1

local FrontKnuckle = game.Workspace.RobotArm.Knuckle
local Knuckles

RS.Heartbeat:Connect(function()
	if Knuckles then
		local newCFrame = FrontKnuckle.PrimaryPart.CFrame * CFrame.Angles(0, -0.05, 0)
		FrontKnuckle:SetPrimaryPartCFrame(newCFrame)
	end
end)

remote.OnServerEvent:Connect(function(plr, state)
	Knuckles = state
end)

It doesn’t move the part though. (The scripts are placed in their respective spots, so is the RemoteEvent I added.)

[Actually, question, I’m not sure if MeshParts have a place to add a PrimaryPart, would I have to put it behind a model to do so?]

Is FrontKnuckle.PrimaryPart set, and is it anchored?

To:

RS.Heartbeat:Connect(function()
	if Knuckles then
		FrontKnuckle.PrimaryPart.CFrame *= CFrame.Angles(0, -0.05, 0)
	end
end)

if FrontKnuckle is a MeshPart try this:

RS.Heartbeat:Connect(function()
	if Knuckles then
		FrontKnuckle.CFrame *= CFrame.Angles(0, -0.05, 0)
	end
end)

It does work now, but it spins from the center of the MeshPart itself.


For example, I’m moving the top front piece, but it should only move up and down from the black piece connecting to the back of it (the knuckle).

Could you share a screenshot of the hierarchy of the model? If the knuckle and finger part are separate you could use CFrame math to offset the rotation.

Never mind, you could use pivots to pivot the rotation around the knuckle.

Edit the pivot from the model menu:
image

Then move the pivot of the front piece so that it is in the middle of the knuckle:
image

I haven’t tested it, but with the way pivots work the code should work once you apply the pivot.

Hope this helps!

  1. I change the pivot, but the movement doesn’t seem to notice it changed, maybe it has to be applied in the code or something.

  2. The longer I hold the key, the faster the rotation goes, instead of staying at a constant speed.

Yep.

Just tested it for myself, realized that pivots run on a separate CFrame system:

RS.Heartbeat:Connect(function()
	if Knuckles then
		local x,y,z = FrontKnuckle:ToOrientation()
		FrontKnuckle:PivotTo(FrontKnuckle:GetPivot() * CFrame.Angles(x, y - 0.05, z))
	end
end)

See if this fix solves the second problem as well.

I’m so sorry :sweat_smile:

RS.Heartbeat:Connect(function()
	if Knuckles then
		local x,y,z = FrontKnuckle.CFrame:ToOrientation()
		FrontKnuckle:PivotTo(FrontKnuckle:GetPivot()* CFrame.Angles(x, y - 0.05, z))
	end
end)

It works now!
But for some reason even having it at 0.05 makes it extremely fast…
Slowing it down would require something like 0.000005, is there a way to fix that?
Something like (1 being really slow as a minimum)

Roblox uses two measurements for angles, radians and degrees.

x, y, z are in radians however 0.05 can be thought of as being in degrees.
we just need to convert 0.05 degrees to radians

the formula for that is:
radians = degrees * pi/180°
0.05 deg = 0.000873 rad | Much smaller

However, Lua has a built-in function to do it for you: math.rad()

Anyways here’s the updated code:

RS.Heartbeat:Connect(function()
	if Knuckles then
		local x,y,z = FrontKnuckle.CFrame:ToOrientation()
		FrontKnuckle:PivotTo(FrontKnuckle:GetPivot()* CFrame.Angles(x, y - math.rad(0.05), z))
	end
end)

Hope it helps!

It does the same thing sadly.

Maybe the multiplier has something to do with it, might be increasing it to a bigger number, but I’m not sure.

(Also forgot to mention it still speeds up exponentially the longer I hold it)

Unsure if CFrame allows additive behavior of angles, but see if replacing * with + fixes it:

		FrontKnuckle:PivotTo(FrontKnuckle:GetPivot() + CFrame.Angles(x, y - math.rad(0.05), z))

Putting the + made it stop moving :sweat_smile:

I fully tested it out and figured out the problem, try this for the PivotTo():

FrontKnuckle:PivotTo(CFrame.Angles(x, y - math.rad(0.05), z) + FrontKnuckle:GetPivot().Position)

It was wayyy slower :sob: