This is a LocalScript of a tool and I was wondering how would I change how far the player can grab? I played with the local radius but that didn't change anything.

I want to make so the player can only grab things from 3 studs away from the player, not the other side of the Baseplate.

How would I do this? Please help!

local player = game:GetService('Players').LocalPlayer
local holding = {}
local radius = 5
local rocks = {}
local keysDown = {}
local platformConnections = {}
local platformToggle = false
local platformFriendly = false
local platform = nil
local bodyGyro = Instance.new('BodyGyro')
local welds = {}
local touchedConnections = {}
local bodyPositions = {}

bodyGyro.maxTorque = Vector3.new(1, 1, 1) * math.huge

function Distance(object, position)
	return ((position - object.Position).magnitude - object.Size.magnitude / 2)
end

function Grab(position, object) ---- This is where the function is used 
	if not holding[object] and object:IsA('BasePart') and not object.Anchored and Distance(object, position) < 5 then 
		local bodyPosition = Instance.new('BodyPosition')

		table.insert(holding, object)

		bodyPosition.maxForce = object:GetMass() * 1318 * Vector3.new(1, 1, 1)
		bodyPosition.position = object.Position + Vector3.new(0, 3, 0)
		bodyPosition.Parent = object
		bodyPositions[object] = bodyPosition

		touchedConnections[object] = object.Touched:connect(function(hit)
			if not hit or not hit.Parent or not hit:IsA('BasePart') or holding[hit] then
				return
			end

			local human = hit.Parent:FindFirstChild('Humanoid')
			if human and human.Parent ~= player.Character and hit.Parent ~= object.Parent then
			human:TakeDamage(((object.Velocity - hit.Velocity).magnitude^1.25 / 120) * object.Size.magnitude)
		end
		end)
elseif object ~= player.Character then
		for _, nextObject in pairs(object:GetChildren()) do
			Grab(position, nextObject)
end --- where it ends
	end
end

function Drop(ignoreTouched)
	for _, part in pairs(holding) do
		if not ignoreTouched and touchedConnections[part] then
			touchedConnections[part]:disconnect()
		end
		if bodyPositions[part] then
			bodyPositions[part]:Destroy()
		end
	end

	holding = {}
	touchedConnections = {}
	bodyPositions = {}
end

function UpdatePosition(mouse)
	if not player.Character or not player.Character:FindFirstChild('HumanoidRootPart') then
		return
	end

	if welds.neck and welds.neck.Parent then
		local direction = (mouse.Hit.p - player.Character.HumanoidRootPart.Position).unit

		welds.neck.C1 = CFrame.new(0, -0.5, 0) * CFrame.Angles(math.acos(direction.Y), math.pi, 0)
		welds.shoulder.C1 = CFrame.new(-0.5, 0.5, 0) * CFrame.Angles(math.acos(direction.Y) + math.pi, math.pi/2, 0)
		bodyGyro.cframe = CFrame.Angles(0, math.atan2(direction.X, direction.Z) + math.pi, 0)
	end

	for _, bodyPosition in pairs(bodyPositions) do
		bodyPosition.position = mouse.Hit.lookVector * radius + player.Character.HumanoidRootPart.Position
	end
end

function SpawnRock(mouse)
	if not player or not player.Character or not player.Character:FindFirstChild('HumanoidRootPart') then
		return
	end

	local part = Instance.new('Part', game.Workspace)
	part.Name = 'Rock'
	part.Transparency = 1
	part.FormFactor = 'Custom'
	part.Size = Vector3.new(2, 2, 2)
	part.CFrame = CFrame.new(mouse.Hit.lookVector * radius + player.Character.HumanoidRootPart.Position + Vector3.new(math.random(-3, 3), math.random(-3, 3), math.random(-3, 3)))

	local mesh = Instance.new('SpecialMesh', part)
	mesh.MeshId = 'http://www.roblox.com/asset/?id=1290033'
	mesh.TextureId = 'http://www.roblox.com/asset/?id=1290030'
	mesh.Scale = Vector3.new(2, 2, 2)

	table.insert(rocks, part)

	Spawn(function()
		for i = 9, 0, -1 do
			part.Transparency = i/10
			wait(0)
		end
	end)
end

function Throw(mouse)
	for _, part in pairs(holding) do
		local throwAmount = (mouse.Hit.p - part.Position).unit * 2
		
		local bodyForce = Instance.new('BodyForce', part)
		bodyForce.force = Vector3.new(0, part:GetMass() * 196.2 - throwAmount.Y, 0)

		local throwVelocity = Instance.new('BodyVelocity')
		throwVelocity.P = 1
		throwVelocity.maxForce = Vector3.new(1, 1, 1) * 2
		throwVelocity.velocity = throwAmount
		throwVelocity.Parent = part

		game:GetService('Debris'):AddItem(part, 20)
	end

	Drop(true)
end

function TogglePlatform()
	if not player or not player.Character or not player.Character:FindFirstChild('Torso') then
		return
	end

	platformToggle = not platformToggle

	if platformToggle then
		platform = Instance.new('Part', player.Character)
		platform.Reflectance = 0
		platform.Transparency = 1
		platform.CanCollide = false
		platform.FormFactor = 'Custom'
		platform.Size = Vector3.new(15, 0.2, 15)
		platform.CFrame = CFrame.new(player.Character.Torso.Position)

		if platformFriendly then
			platform.BrickColor = BrickColor.new('White')
		else
			platform.BrickColor = BrickColor.new('White')
		end

		Instance.new('CylinderMesh', platform)
		Instance.new('BodyForce', platform).force = Vector3.new(0, platform:GetMass() * 196.2, 0)

		local platformWeld = Instance.new('Weld', platform)
		platformWeld.Part0 = player.Character.Torso
		platformWeld.Part1 = platform
		platformWeld.C0 = CFrame.new(0, -3.05, 0)

		for _, part in pairs(player.Character:GetChildren()) do
			if part:IsA('BasePart') then
				platformConnections[part] = part.Touched:connect(function(hit)
					if platformFriendly then
						return
					end
					if not player.Character or not player.Character:FindFirstChild('Torso') then
						return
					end
					if not hit or not hit.Parent or not hit.Parent:FindFirstChild('Torso') or hit.Parent == player.Character then
						return
					end
					
					local human = hit.Parent:FindFirstChild('Humanoid')
					local throwForce = Instance.new('BodyForce', hit.Parent.Torso)
					
					throwForce.force = (hit.Parent.Torso.Position - player.Character.Torso.Position).unit * 1000 + Vector3.new(0, 50, 0)
					
					if human then
						human.PlatformStand = true
					end
					
					wait(0.5)
					
					throwForce:Destroy()
					
					if human then
						human.PlatformStand = false
					end
				end)
			end
		end
	else
		if platform then
			for _, connection in pairs(platformConnections) do
				connection:disconnect()
			end
			
			platform:Destroy()
			
			platformConnections = {}
		end
	end
end

function KeyPress(key, isDown, callback)
	keysDown[key] = isDown
	
	if isDown then
		callback()
		
		wait(0.35)
		
		while keysDown[key] do
			callback()
			
			wait(0.15)
		end
	end
end

script.Parent.Selected:connect(function(mouse)
	mouse.Icon = 'rbxasset://textures/GunCursor.png'

	if player and player.Character and player.Character:FindFirstChild('Torso') then
		bodyGyro.Parent = player.Character.Torso

		if player.Character.Torso:FindFirstChild('Neck') then
			welds['neck'] = player.Character.Torso.Neck
		end

		if player.Character.Torso:FindFirstChild('Right Shoulder') then
			welds['shoulder'] = player.Character.Torso['Right Shoulder']
		end
	end

	mouse.Button1Down:connect(function()
		if not mouse.Target or not player.Character or not player.Character:FindFirstChild('HumanoidRootPart') then
			return
		end

		Grab(mouse.Hit.p, game.Workspace)  --- Is where the player grabs the object here i want it so the grab distance, on when you click, to have a max distance like, the player has to be 3 or something studs nearby in order for the Grab function to work.

	end)

	mouse.Button1Up:connect(function()
		Drop(false)
	end)

	mouse.Idle:connect(function()
		UpdatePosition(mouse)
	end)
	mouse.Move:connect(function()
		UpdatePosition(mouse)
	end)

	mouse.KeyDown:connect(function(key)
		if key == 'q' then
			KeyPress(key, true, function()
				radius = radius - 2
			end)
		elseif key == 'e' then
			KeyPress(key, true, function()
				radius = radius + 2
			end)			
			rocks = {}
		elseif key == 'f' then
			Throw(mouse)
		elseif key == 'x' then
			TogglePlatform()
		elseif key == '' then
			if player and player.Character and player.Character:FindFirstChild('Torso') and mouse.Hit then
				player.Character.Torso.CFrame = mouse.Hit + Vector3.new(0, 3, 0)
			end
		end
	end)
	
	mouse.KeyUp:connect(function(key)
		KeyPress(key, false)
	end)
end)

script.Parent.Deselected:connect(function()
	if welds.neck then
		welds.neck.C0 = CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)
		welds.neck.C1 = CFrame.new(0, -0.5, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)
	end

	if welds.shoulder then
		welds.shoulder.C0 = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
		welds.shoulder.C1 = CFrame.new(-0.5, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
	end

	welds = {}
	bodyGyro.Parent = nil
end)

2 comments

0

Where is the part of the script where it grabs the object? Only that part is useful to us - the rest just makes it harder.

0

Line 21 is the Grab function is, and line 239 has relevance to it to and I just edited it