Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does RunService:RenderStepped seem like it has two separate instances?

player.CharacterAdded:Connect(function(character)
 
	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")
	humanoid.AutoRotate = false
 
	local cameraAngleX = 0
	local cameraAngleY = 0
 
	local function playerInput(actionName, inputState, inputObject)
		-- Calculate camera/player rotation on input change
		print("input change")
		if inputState == Enum.UserInputState.Change then

			cameraAngleX = cameraAngleX - inputObject.Delta.X
			-- Reduce vertical mouse/touch sensitivity and clamp vertical axis
			cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
			-- Rotate root part CFrame by X delta
			rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
		end
	end
	ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
 
	RunService.RenderStepped:Connect(function()
		if camera.CameraType ~= Enum.CameraType.Scriptable then
			camera.CameraType = Enum.CameraType.Scriptable
		end
		local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
		local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
		print("Parent", script.Parent, "CFrame", rootPart.CFrame.Position)
	end)
end)

This is some code taking from the Roblox Dev Article https://developer.roblox.com/en-us/articles/Camera-manipulation in the "Over the Shoulder" section.

playerInput should fire everytime the mouse is moving, printing "Input Changed" Renderstepped should fire every frame, printing the player's position.

This code works usually, but when the player dies, a new RenderStepped starting printing the respawned player's position, but it seems as if the old one is still printing the player's position before it died. (See below)

Output:

  input change
  Parent PlayerScripts CFrame -113.862038, 2.97397137, -88.958931
  Parent PlayerScripts CFrame -26.2728577, -509.415833, -313.633331
  input change
  Parent PlayerScripts CFrame -114.072174, 2.97397137, -89.1231079
  Parent PlayerScripts CFrame -26.2728577, -509.415833, -313.633331

As you can see, the first line of each block is inputChange from me moving my mouse.

The second line is the player's new position after they respawn. This changes as I move the player.

The third line is the player's position right before it died. Here, it seems like two instances of RenderedStep is running.

Can anyone explain why?

1 comment

0

You never disconnect the connection

1 answer

I usually use this more.

game:service("RunService").Stepped:wait(0)

Maybe give it a shot?

I'd suggest making it a function, like as if you were creating a renderstep wait.

function renderwait(num)
	if num == 0 or num == nil then
		game:service("RunService").Stepped:wait(0)
	else
		for i = 0, num do
			game:service("RunService").Stepped:wait(0)
		end
	end
end

2 comments

0

The Wait method should be capitalized, service should be GetService, and the Wait method takes no args

0

works for me either way

This discussion is preserved as it was. New answers, comments, and votes are closed.