I've been trying to make an Anti-Noclip script, but it doesn't work too well. Could someone help me or tell me what's wrong and how to fix it?
This is the code I currently have:
local function CheckNoclip(previous_pos: CFrame, current_pos: CFrame, player: string)
local raycastResult = workspace:Raycast(current_pos, previous_pos)
local char = workspace:FindFirstChild(player)
if raycastResult then
print("Instance:", raycastResult.Instance)
print("Position:", raycastResult.Position)
print("Distance:", raycastResult.Distance)
print("Material:", raycastResult.Material)
print("Normal:", raycastResult.Normal)
end
if raycastResult then
if raycastResult.Instance.CanCollide == true and (not (raycastResult.Instance:IsDescendantOf(char)) ) then
local Physical_Part = workspace:FindFirstChild(player)
local RootPart = Physical_Part:WaitForChild("HumanoidRootPart")
RootPart.CFrame = CFrame.new(previous_pos)
warn("No-Clip Found")
end
print("---------------------------------")
end
end
local players = game.Players
players.PlayerAdded:Connect(function(plr: Player)
local player_name = plr.Name
plr.CharacterAdded:Wait()
local RootPart = workspace:FindFirstChild(player_name).HumanoidRootPart
local prev_position = RootPart.CFrame.Position
local current_position = prev_position
while true do
current_position = RootPart.CFrame.Position
if prev_position ~= current_position then
CheckNoclip(prev_position, current_position, player_name)
prev_position = current_position
end
wait()
end
end)
This keeps happening: https://youtu.be/5hF6SoNxX08 (I uploaded it as an unlisted youtube video)
The problem is that you gave the wrong parameters the :Raycast()
method. the function needs an Orgin
and Direction
not a Start
And End
you can calculate the Direction between these 2 vectors by doing CurrPos - LastPos
.
note that this could possible false flag because of lag or ping since its serversided.
local exclude = {}
local function CheckNoclip(LastPos: Vector3, CurrPos: Vector3, Player: Player)
if CurrPos == LastPos then return; end
local Orgin = LastPos;
local Direction = CurrPos - LastPos;
local Params = RaycastParams.new();
Params.FilterDescendantsInstances = exclude;
Params.FilterType = Enum.RaycastFilterType.Exclude;
local Result = workspace:Raycast(Orgin, Direction, Params);
if not Result then return; end
if not Result.Instance.CanCollide then return; end
local Character = Player.Character;
local Root = Character:FindFirstChild("HumanoidRootPart");
Root.CFrame = CFrame.new(LastPos);
warn("No-Clip Found");
print("---------------------------------");
end
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
Player.CharacterAdded:Connect(function(Character)
table.insert(exclude, Character);
local Humanoid = Character:WaitForChild("Humanoid");
local Root = Character:WaitForChild("HumanoidRootPart");
local CurrPos = Root.Position;
local LastPos = CurrPos;
task.spawn(function() -- create loop in new thread
repeat
LastPos = CurrPos;
CurrPos = Root.Position;
CheckNoclip(LastPos, CurrPos, Player);
task.wait();
until Humanoid.Health <= 0
end)
end);
Player.CharacterRemoving:Connect(function(Character)
table.remove(exclude, table.find(exclude, Character));
end);
end)