Hello everyone! I am making a game where you are a character with noodles for your arms. I got all of it working and it works pretty well. But I feel that it can be improved upon in a few ways that I am just missing so please let me know how I can improve it!
(P.S there is a bug where the character you are grabbing will reset to their prior grab position, so if you can find a fix for that, I’d also appreciate it!)
local players = game:GetService("Players")
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local debrisService = game:GetService("Debris")
local camera = workspace.CurrentCamera
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local modules = replicatedStorage:WaitForChild("Modules")
local objects = require(modules.Objects)
local root = character:WaitForChild("Torso")
local rightHand = character:WaitForChild("RightNoodle")
local leftArm = character:WaitForChild("LeftNoodle")
local rightHand = rightHand:WaitForChild("Hand")
local leftHand = leftArm:WaitForChild("Hand")
local right = false
local left = false
local welded = false
local jump_debounce = false
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {rightHand, leftArm, character}
camera.CameraType = Enum.CameraType.Follow
local otherCharacter = nil
-- camera.CameraSubject = character:WaitForChild("Torso")
function preWeld()
for _, part in ipairs(otherCharacter:GetChildren()) do
if part:IsA("Humanoid") then
part.PlatformStand = true
elseif part:IsA("BasePart") then
-- part.Massless = true
part.Anchored = false
end
end
end
function postWeld()
for _, part in ipairs(otherCharacter:GetChildren()) do
if part:IsA("Humanoid") then
part.PlatformStand = false
elseif part:IsA("BasePart") then
-- part.Massless = false
end
end
end
userInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
local mousePos = userInputService:GetMouseLocation()
if input.UserInputType == Enum.UserInputType.MouseButton2 then
local unitRay = camera:ScreenPointToRay(mousePos.X, mousePos.Y)
local ray = game.Workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
if ray then
if (rightHand.Position - ray.Position).Magnitude <= 35 then
if ray.Instance.Parent:FindFirstChild("Humanoid") then
otherCharacter = ray.Instance.Parent
print(otherCharacter.Name)
local Weld = Instance.new("WeldConstraint")
Weld.Part0 = rightHand
Weld.Part1 = otherCharacter.Torso
Weld.Parent = rightHand
right = true
welded = true
preWeld()
else
objects.clear(rightHand)
objects.add("BodyPosition", rightHand, ray.Position)
right = true
end
end
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
local unitRay = camera:ScreenPointToRay(mousePos.X, mousePos.Y)
local ray = game.Workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
if ray then
if (leftHand.Position - ray.Position).Magnitude <= 35 then
if ray.Instance.Parent:FindFirstChild("Humanoid") then
otherCharacter = ray.Instance.Parent
print(otherCharacter.Name)
local Weld = Instance.new("WeldConstraint")
Weld.Part0 = leftHand
Weld.Part1 = otherCharacter.Torso
Weld.Parent = leftHand
left = true
welded = true
preWeld()
else
objects.clear(leftHand)
objects.add("BodyPosition", leftHand, ray.Position)
left = true
end
end
end
elseif input.KeyCode == Enum.KeyCode.Y then
if (right == false and left == false) or welded == true then
objects.add("VectorForce", rightHand)
objects.add("VectorForce", leftHand)
else
if right == false then
objects.add("VectorForce", rightHand)
elseif left == false then
objects.add("VectorForce", leftHand)
end
end
elseif input.KeyCode == Enum.KeyCode.Space then
if right or left and jump_debounce == false then
debrisService:AddItem(objects.add("BodyVelocity", root), 0.35)
jump_debounce = true
coroutine.wrap(function()
wait(2)
jump_debounce = false
end)()
end
end
end)
userInputService.InputEnded:Connect(function(input, gpe)
if gpe then return end
local mousePos = userInputService:GetMouseLocation()
if input.UserInputType == Enum.UserInputType.MouseButton2 then
objects.clear(rightHand, "BodyPosition")
objects.clear(character, "WeldConstraint")
if otherCharacter then
postWeld()
welded = false
otherCharacter = nil
end
right = false
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
objects.clear(leftHand, "BodyPosition")
objects.clear(character, "WeldConstraint")
if otherCharacter then
postWeld()
welded = false
otherCharacter = nil
end
left = false
elseif input.KeyCode == Enum.KeyCode.Y then
objects.clear(rightHand, "VectorForce")
objects.clear(leftHand, "VectorForce")
-- objects.clear(leftHand, "WeldConstraint")
end
end)