Hey. I wanna' do a Grapple Hook that activates when I press F and takes you to the object your mouse is on. I got the script working and all(like if I want to grapple to a specific part everything works) but when I try it with the mouse it says Workspace.bruceywayne.NEW:25: attempt to index field 'Target' (a nil value) and I don't know why. This is the script:

player = game.Players.LocalPlayer
mouse = player:GetMouse()
target = mouse.Target

activated = true

local character = game.Players.LocalPlayer.Character
Humanoid = character.Humanoid

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=01231272033"

local animation2 = Instance.new("Animation")
animation2.AnimationId = "http://www.roblox.com/Asset?ID=01231293787"

local animTrack = Humanoid:LoadAnimation(animation)
local animTrack2 = Humanoid:LoadAnimation(animation2)

local GrappleSound = Instance.new("Sound", workspace)
GrappleSound.SoundId = "http://www.roblox.com/Asset?ID=996815728"
GrappleSound.Volume = 1

mouse.KeyDown:Connect(function(key)
	for i, v in pairs(game.Players:GetChildren()) do
		if (v.Character.UpperTorso.Position - mouse.Target.Position).magnitude <= 100 then
			if string.lower(key) == 'f' and activated == true then
				local Grapple = game.Players.LocalPlayer.Character.rhand
				if character and character:findFirstChild("Humanoid") then
					Grapple.Union.Transparency = 0
					activated = false
					animTrack:Play()
					GrappleSound:Play()
					local partA = Grapple.PartA
					local attachmentA = Instance.new("Attachment", partA)
					attachmentA.Position = Vector3.new(0, 0, -1)
				
					local partB = target
					local attachmentB = Instance.new("Attachment", partB)
					attachmentB.Position = Vector3.new(0, .5, 0)
				
					local Rope = Humanoid.Parent.rhand.Beam
					Rope.Attachment0 = attachmentA
					Rope.Attachment1 = attachmentB
					Rope.Segments = 1
					Rope.Width0 = 0.3
					Rope.Width1 = 0.3
				
					wait(1)
				
					animTrack:Stop()
					animTrack2:Play()
				
					local b = Instance.new("BodyPosition")
        			b.position = mouse.Target.Position
        			b.maxForce = Vector3.new(500000000, 500000000, 500000000)
        			b.Parent = character.UpperTorso	
				
					wait(1.5)
				
					Rope.Attachment0 = nil
					Rope.Attachment1 = nil
        			b.Parent = nil
					activated = true
					animTrack2:Stop()
					Grapple.Union.Transparency = 1
				end
			end
		end
	end
end)

5 comments

0

Have you tried using mouse.X and mouse.Y? You can use these 2 positions to chart where the mouse's coordinates are.

0

KeyDown is deprecated, do NOT use it in new work.

0

Then what should I use? Also I'll try using mouse.X and mouse.Y.

0

KeyDown still works, but it's deprecated, meaning it'll eventually completely faze out. You can check the DevHub on UserInputService: http://robloxdev.com/articles/Keyboard-Input-Methods

0

Used UserInputService and I get the same error: Workspace.bruceywayne.NEW:25: attempt to index field 'Target' (a nil value)