Help replicating hingeconstraint

i’m working on a sandbox game that has motors in it
the motors use hinge constraints to turn, and i’m using a localscript inside playergui to turn the hinge constraint

the local player can see the motor turn, but other players cant because the hingeconstraint is being set on the client side

i can not use a remote event and fire a desired angle to the server because the game already suffers from some server lag

is there any optimal way to replicate a hingeconstraint to the server?

3 Likes

That shouldn’t be happening. Physics should replicate this. Are you sure your assembly’s network owner is set?

1 Like

Why are you using a localscript to do it? Please post the script that activates the HingeConstraint.

The HingeConstraint should be in the workspace and should be controlled by a serverscript there.

1 Like

the game functions by placing objects and spawning them in and the players interact with the motors by pressing keys

all the models in the player’s build when spawning in are cloned and the copy has its networkowner set to the player since they’re being run off of localscripts

i hope that gives more context on what’s going on

1 Like
local uis = game:GetService("UserInputService")
local block = script.Block.Value
local canturn = true
local val = 2

wait(1)

val = block.Config.Speed.Value*2
block.Base.HingeConstraint.MotorMaxTorque = block.Config.MaxTorque.Value*900

block.Base.WeldConstraint:Destroy()

if block.Config.Visible.Value == false then
	block.Base.ChangeAmbience:FireServer({Visible = false})
end

if block.Config.Servo.Value == true then
	block.Base.HingeConstraint.ActuatorType = Enum.ActuatorType.Servo
	block.Base.HingeConstraint.AngularSpeed = val
	block.Base.HingeConstraint.ServoMaxTorque = block.Config.MaxTorque.Value*900
end

if block.Config.Powered.Value == false then
	block.Base.HingeConstraint.ActuatorType = Enum.ActuatorType.None
end

-- define some variables

function began(i)
	
	if block.Config.Mouse.Value == true and block.Config.Servo.Value == true then
		return
	end
	
	if game.Players.LocalPlayer.Character.Humanoid.Sit == false then
		return
	end
	
	if canturn == false then
		return
	end
	
	if i.KeyCode.Name == script.Block.Value.Config.NegativeTurn.Value then
		
		
		if block.Base.HingeConstraint.ActuatorType == Enum.ActuatorType.Motor then
			local proceed = true
			if script.Block.Value.Config.ToggleMode.Value == true then
				
				if block.Base.HingeConstraint.AngularVelocity == val then
					block.Base.HingeConstraint.AngularVelocity = 0
					proceed = false
				end
			end
			
			if proceed == true then
				script.Block.Value.Base.HingeConstraint.AngularVelocity = val
			end
		else
			local proceed = true
			if script.Block.Value.Config.ToggleMode.Value == true then
				if block.Base.HingeConstraint.TargetAngle == val then
					block.Base.HingeConstraint.TargetAngle = 0
					proceed = false
				end
			end
			
			if proceed == true then
				block.Base.HingeConstraint.TargetAngle = 25
			end
		end
	elseif i.KeyCode.Name == script.Block.Value.Config.PositiveTurn.Value then
		
		if block.Base.HingeConstraint.ActuatorType == Enum.ActuatorType.Motor then
			local proceed = true
			if script.Block.Value.Config.ToggleMode.Value == true then
				if block.Base.HingeConstraint.AngularVelocity == -val then
					block.Base.HingeConstraint.AngularVelocity = 0
					proceed = false
				end
			end
			
			if proceed == true then
				script.Block.Value.Base.HingeConstraint.AngularVelocity = -val
			end
		else
			local proceed = true
			if script.Block.Value.Config.ToggleMode.Value == true then
				if block.Base.HingeConstraint.TargetAngle == -val then
					block.Base.HingeConstraint.TargetAngle = 0
					proceed = false
				end
			end
			
			if proceed == true then
				block.Base.HingeConstraint.TargetAngle = -25
			end
		end
	end
end

function ended(i)
	
	if block.Config.Mouse.Value == true and block.Config.Servo.Value == true then
		return
	end


	if game.Players.LocalPlayer.Character.Humanoid.Sit == false then
		return
	end
	
	if canturn == false then
		return
	end
	
	if script.Block.Value.Config.ToggleMode.Value == true then
		return
	end
	
	if i.KeyCode.Name == script.Block.Value.Config.NegativeTurn.Value then
		if block.Base.HingeConstraint.ActuatorType == Enum.ActuatorType.Motor then
			if script.Block.Value.Base.HingeConstraint.AngularVelocity > 0 then
				script.Block.Value.Base.HingeConstraint.AngularVelocity = 0
			end
		else
			block.Base.HingeConstraint.TargetAngle = 0
		end
	elseif i.KeyCode.Name == script.Block.Value.Config.PositiveTurn.Value then
		if block.Base.HingeConstraint.ActuatorType == Enum.ActuatorType.Motor then
			if script.Block.Value.Base.HingeConstraint.AngularVelocity < 0 then
				script.Block.Value.Base.HingeConstraint.AngularVelocity = 0
			end
		else
			block.Base.HingeConstraint.TargetAngle = 0
		end
	end
end


uis.InputBegan:Connect(function(i,e)
	if e == true then
		return
	end
	
	began(i)
end)

uis.InputEnded:Connect(function(i,e)
	
	ended(i)
end)



game.Players.LocalPlayer.PlayerGui.Keys.Action.Event:Connect(function(i,t)
	if t == true then
		began({KeyCode = {Name = i}})
	else
		ended({KeyCode = {Name = i}})
	end
end)
1 Like

the network ownership is set to the player

So do you or don’t you want the other players to see the HingeConstraint items turning? You stated that that’s what’s happening but didn’t make if clear if that’s desired or not.

Please give us a better in-game explanation of what you are trying to do. For example, a builder places balance beams on their baseplate and when they click on a Gui the balance beam levels out, or pivots one way or the other.

From what I can see it looks like you are using this single script for whatever item is placed, whether it’s something like a door which sets the Servot TargetAngle to 25 or 0 or a rotating item in which case you are setting the Motor AngularVelocity to 0 depending on if it’s positive or negative.

If you need everyone to see it happen then the HingeConstraint needs to be server sided. If only the builder can control it then the Gui button input should only be received from the builder’s buttons.