I was trying to make it so, the parent of this script always stands 2.5 studs above the floor part. After coding the script, i saw that it didn’t work. I couldn’t find why, too.
--Services
local RunService = game:GetService("RunService")
--Objects
local Part = script.Parent
local Character = Part.Parent.Parent
--Values
local Threshold = 2048
if Character:IsA("Model") and Character:FindFirstChildOfClass("Humanoid") then
local function IsPartInDirection()
local Return = false
local FoundPart = nil
pcall(function()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Part, Character}
raycastParams.IgnoreWater = true
raycastParams.RespectCanCollide = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local direction = Vector3.new(0, -1, 0)
local origin = Part.Position -- Corrected origin calculation
local raycastResult = workspace:Raycast(origin, direction * Threshold, raycastParams)
if raycastResult then
Return = true
FoundPart = raycastResult.Instance
end
end)
return Return, FoundPart
end
RunService.Heartbeat:Connect(function()
local Check, FoundPart = IsPartInDirection()
if Check then
print(FoundPart)
local PartDistance = FoundPart.Position.Y + FoundPart.Size.Y/2 -- Corrected calculation
local Distance = PartDistance + 3 -- Adjusted calculation
Part.Position = Vector3.new(Part.Position.X, Distance, Part.Position.Z) -- Apply corrected position
end
end)
end
The issue was that the part wasnt setting the position correctly even though the position it calculated was right. I found the issue, and it was caused because im welding the part with a motor6d, but it seems to still be broken. Here is the updated code:
--Services
local RunService = game:GetService("RunService")
--Objects
local Part = script.Parent
local Character = Part.Parent.Parent
local PartMotor = script.Parent
--Values
local Threshold = 2048
if Character:IsA("Model") and Character:FindFirstChildOfClass("Humanoid") and script.Parent:FindFirstChild("HumanoidRootPartMotor") then
local PartMotor: Motor6D = script.Parent:FindFirstChild("HumanoidRootPartMotor")
local function IsPartInDirection()
local Return = false
local FoundPart = nil
pcall(function()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Part, Character}
raycastParams.IgnoreWater = true
raycastParams.RespectCanCollide = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local direction = Vector3.new(0, -1, 0)
local origin = Part.Position -- Corrected origin calculation
local raycastResult = workspace:Raycast(origin, direction * Threshold, raycastParams)
if raycastResult then
Return = true
FoundPart = raycastResult.Instance
end
end)
return Return, FoundPart
end
RunService.Heartbeat:Connect(function()
local Check, FoundPart = IsPartInDirection()
if Check then
local FoundPartTopPos = FoundPart.Position.Y + FoundPart.Size.Y/2 -- Corrected calculation
local PartBottomPos = Part.Position.Y - Part.Size.Y/2
PartMotor.C0.Position = Vector3.new(0, PartBottomPos - FoundPartTopPos, 0)
end
end)
end