Confusion on Suphi Kaner Tutorial Inverse Kinematics

I’m trying to learn inverse kinematics but I don’t understand why he scripted the CharacterIK in this way.

Local script in StarterPlayerScripts:

local RunService = game:GetService("RunService")
local parts = {}

local function CharacterAdded(character)
	task.wait()
	local ik = Instance.new("IKControl")
	ik.Type = Enum.IKControlType.LookAt
	ik.ChainRoot = character.UpperTorso
	ik.EndEffector = character.Head
	ik.Target = parts[game.Players:GetPlayerFromCharacter(character)]
	ik.Weight = 0.555
	ik.Parent = character.Humanoid
	
	character.Humanoid.Died:Wait() ik:Destroy()
end

local function PlayerAdded(player)
	local part = Instance.new("Part")
	part.CanCollide = false
	part.CanQuery = false
	part.CanTouch = false
	part.Transparency = 1
	part.Anchored = true
	part.Parent = workspace
	parts[player] = part
	
	if player.Character ~= nil then CharacterAdded(player.Character) end
	player.CharacterAdded:Connect(CharacterAdded)
end
local function PlayerRemoving(player)
	parts[player]:Destroy()
	parts[player] = nil
end

for i, player in game.Players:GetPlayers() do PlayerAdded(player) end
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)

RunService.RenderStepped:Connect(function(deltaTime)
	for player,part in parts do
		if player.Character == nil then continue end
		part.Position = player.Character.Head.Position + player:GetAttribute("LookVector") * 10
	end
end)

Tutorial

I don’t understand why he uses indexes of players in his parts table in a local script and why PlayerAdded/CharacterAdded events are being used on the client.

1 Like

the server tells the client the angle the players character is looking in

then locally each player makes a part for all players and updates the position of this part to make them face in the direction that the server set

so we know what part is used for each player each part is stored in a table and we use the player as a key so we know what part corresponds to what player

The reason this is done locally is to reduce network data if the server updated the position of the part not only would it use more network data it would also be laggy making the characters head jitter

1 Like