You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want the players humanoidrootpart not to be affected and the companions to be properly welded -
What is the issue? Include screenshots / videos if possible!
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local function updateCompanion(companion: Model, player: Player, index, totalCompanions)
local character = player.Character
if not character then return end
local companionFolder = companion.Parent
local playerRoot: BasePart = character:FindFirstChild("HumanoidRootPart")
local companionRootPart: BasePart = companion:FindFirstChild("HumanoidRootPart")
local playerHumanoid: Humanoid = playerRoot.Parent:FindFirstChild("Humanoid")
local companionHumanoid: Humanoid = companion:FindFirstChild("Humanoid")
local unitName = companion:GetAttribute("UnitName")
if not playerRoot or not companionRootPart or not playerHumanoid or not companionHumanoid then
return
end
if unitName == nil then
warn("Unit Name is nil")
return
end
local function setupAnims()
local runAnimation: Animation
local walkAnimation: Animation
local idleAnimation: Animation
local companionAnimationFolder: Folder = animationFolder:FindFirstChild(unitName)
if not companionAnimationFolder then
companionAnimationFolder = defaultAnimationFolder
end
runAnimation = companionAnimationFolder:FindFirstChild("Run")
walkAnimation = companionAnimationFolder:FindFirstChild("Walk")
idleAnimation = companionAnimationFolder:FindFirstChild("Idle")
if not runAnimation or not walkAnimation or not idleAnimation then
warn("runAnimation or walkAnimation or idleAnimation is nil for unit "..unitName)
return
end
if animations[companionHumanoid]["Run"] == nil then
animations[companionHumanoid]["Run"] = companionHumanoid:LoadAnimation(runAnimation)
end
if animations[companionHumanoid]["Walk"] == nil then
animations[companionHumanoid]["Walk"] = companionHumanoid:LoadAnimation(walkAnimation)
end
if animations[companionHumanoid]["Idle"] == nil then
animations[companionHumanoid]["Idle"] = companionHumanoid:LoadAnimation(idleAnimation)
end
end
if animations[companionHumanoid] == nil then
animations[companionHumanoid] = {}
setupAnims()
end
if animations[companionHumanoid]["Run"] == nil or animations[companionHumanoid]["Walk"] == nil or animations[companionHumanoid]["Idle"] == nil then
setupAnims()
end
local runAnimation: AnimationTrack = animations[companionHumanoid]["Run"]
local walkAnimation: AnimationTrack = animations[companionHumanoid]["Walk"]
local idleAnimation: AnimationTrack = animations[companionHumanoid]["Idle"]
local moving = false
if playerHumanoid.MoveDirection.Magnitude > 0 then
moving = true
else
moving = false
end
if not moving then
if runAnimation.IsPlaying then
runAnimation:Stop()
end
if walkAnimation.IsPlaying then
walkAnimation:Stop()
end
if not idleAnimation.IsPlaying then
idleAnimation:Play()
end
else
if idleAnimation.IsPlaying then
idleAnimation:Stop()
end
if playerHumanoid.WalkSpeed > 16 then
if walkAnimation.IsPlaying then
walkAnimation:Stop()
end
if not runAnimation.IsPlaying then
runAnimation:Play()
end
else
if runAnimation.IsPlaying then
runAnimation:Stop()
end
if not walkAnimation.IsPlaying then
walkAnimation:Play()
end
end
end
local spreadAngle = (index - 1) * (2 * math.pi / totalCompanions)
local spreadRadius = 5 -- Adjust this to change the distance from the player
local companionOffset = Vector3.new(math.cos(spreadAngle), 0, math.sin(spreadAngle)) * spreadRadius
local targetPosition = playerRoot.Position + companionOffset -- Calculate the position around the player
-- Raycasting to ensure companion lands on the ground
--local raycastParams = RaycastParams.new()
--raycastParams.IgnoreWater = true
--raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
--raycastParams.FilterDescendantsInstances = {companion, player.Character}
--local rayStart = targetPosition + Vector3.new(0, 10, 0) -- Start the ray 20 studs above the target position
--local rayDirection = Vector3.new(0, -100, 0) -- Cast the ray downwards
--local raycastResult = workspace:Raycast(rayStart, rayDirection, raycastParams)
--if raycastResult then
-- targetPosition = raycastResult.Position + Vector3.new(0, 1, 0) -- Add a small offset to the Y-coordinate
--end
-- Find or create the companion weld
local weld: Weld = playerRoot:FindFirstChild(companion.Name)
if not weld then
weld = Instance.new("Weld", playerRoot)
weld.Name = companion.Name
end
weld.Part0 = playerRoot
weld.Part1 = companionRootPart
-- Calculate the desired CFrame for the companion relative to the player's position
local relativeCFrame = CFrame.new(targetPosition - playerRoot.Position)
weld.C1= relativeCFrame
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.