I need help with my grapple gun

Ok, so I have been working on a grapple gun and there is an error, I couldn’t fix it and I need your help… can you guys help me?

Here is the code:

This is in a MODULE script

local RunService = game:GetService("RunService")
local players = game:GetService("Players")

local Tool = script.Parent
local base = Tool:WaitForChild("Base")
local spring = base.Spring
local hook = Tool:WaitForChild("Hook")
local hookRefrence = Tool:WaitForChild("HookReference")
local hookWeld = hook.HookWeld
local messageEvent = script:WaitForChild("MessageEvent")
local activated = script:WaitForChild("Activated")

-- ONLY CLIENT
local player = nil	
local mouse = nil -- we don't want the client to give variables to the server


local GrappleGun = {}

local function clientInit()
	player = players.LocalPlayer
	mouse = player:GetMouse()
	
	mouse.TargetFilter = player.Character -- this will prevent the player getting hit with the gun
	
	Tool.Activated:Connect(function()
		if not GrappleGun.IsActivated() and mouse.Target then
			messageEvent:FireServer(mouse.Hit.Position)
		end
		
	end)
	
	Tool.Deactivated:Connect(function()
		messageEvent:FireServer(false)
	end)
	
	Tool.Unequipped:Connect(function()
		messageEvent:FireServer(false)
	end)
	
end

local function ServerInit()
	
	messageEvent.OnServerEvent:Connect(function(player, _activated, targetPosition)
		activated.Value = _activated
		
		if GrappleGun.IsActivated() then
			GrappleGun.Fire(targetPosition)
		else
			GrappleGun.Reset()
		end
		
		
	end)
	
end

function init()
	if RunService:IsClient() then
		clientInit()
	elseif RunService:IsServer() then
		ServerInit()
		end
end


function GrappleGun.IsActivated()
	return activated.Value
end

function  GrappleGun.Fire(targetPosition)
	local initalPos = hookRefrence.Position
	
	local distance = (targetPosition - initalPos).Magnitude -- THIS IS LINE 75
	
	hookWeld.Enabled = false
	hook.Anchored = true
	
	wait()
	
	hook.Position = targetPosition
	spring.FreeLength = distance
	
end

function GrappleGun.Reset()
	print("Reseted")
end


init()
return GrappleGun

Then we have a SCRIPT And LOCAL SCRIPT THAT JUST HAS THESE

local GrappleGun = require(script.Parent)

can you please help?

and then it has this error
Players.Inferno9878.Backpack.GrappleGun.GrappleGunModule:75: invalid argument #1 (Vector3 expected, got nil)

Can you quote which line is line 75, don’t feel like counting the lines to see which one it is :sweat_smile:

I actially did

its like in the middle

it has a comment on it

Instead of using . can you use a : when writing functions to a list so do this to the rest of the functions like so…

function GrappleGun:Fire(targetPosition)

What I think is occuring due to this simple syntax error its causing the function to run, but since you haven’t given a targetPosition it replaces it as nil

oh ok so then what would I do to fix it?

I made all the functions have like functions:Fire or with the colan but it is still having the same error

Can I see the new script with this change, sorry if i’m being extra here…

its fine :slight_smile: let me pull it up

one sec


local RunService = game:GetService("RunService")
local players = game:GetService("Players")

local Tool = script.Parent
local base = Tool:WaitForChild("Base")
local spring = base.Spring
local hook = Tool:WaitForChild("Hook")
local hookRefrence = Tool:WaitForChild("HookReference")
local hookWeld = hook.HookWeld
local messageEvent = script:WaitForChild("MessageEvent")
local activated = script:WaitForChild("Activated")

-- ONLY CLIENT
local player = nil	
local mouse = nil -- we don't want the client to give variables to the server


local GrappleGun = {}

local function clientInit()
	player = players.LocalPlayer
	mouse = player:GetMouse()
	
	mouse.TargetFilter = player.Character -- this will prevent the player getting hit with the gun
	
	Tool.Activated:Connect(function()
		if not GrappleGun.IsActivated() and mouse.Target then
			messageEvent:FireServer(mouse.Hit.Position)
		end
		
	end)
	
	Tool.Deactivated:Connect(function()
		messageEvent:FireServer(false)
	end)
	
	Tool.Unequipped:Connect(function()
		messageEvent:FireServer(false)
	end)
	
end

local function ServerInit()
	
	messageEvent.OnServerEvent:Connect(function(player, _activated, targetPosition)
		activated.Value = _activated
		
		if GrappleGun.IsActivated() then
			GrappleGun:Fire(targetPosition)
		else
			GrappleGun:Reset()
		end
		
		
	end)
	
end

function init()
	if RunService:IsClient() then
		clientInit()
	elseif RunService:IsServer() then
		ServerInit()
		end
end


function GrappleGun:IsActivated()
	return activated.Value
end

function  GrappleGun:Fire(targetPosition)
	local initalPos = hookRefrence.Position
	
	local distance = (targetPosition - initalPos).Magnitude
	
	hookWeld.Enabled = false
	hook.Anchored = true
	
	wait()
	
	hook.Position = targetPosition
	spring.FreeLength = distance
	
end

function GrappleGun:Reset()
	print("Reseted")
end


init()
return GrappleGun

Why do you have your client code in a module script? I also only see you passing on 1 variable in messageEvent, but in ServerInit() you are getting 3 variables and passing them on to the line that produces the error

This script is from a tutorial, I saw this before.

So ok the changes done were correct now the only reasonable error

is this

can you print print target position please

I don’t really know, I am following B ricey so then my main thing was to be better at scripting with roblox physics, but then he told me that this would work

CreepingGamingTVhmmm, thats weird, it says that the target is nil, how is it nil?

You’re code is very unclear so I can’t tell how this was logically designed though you must go back to where you fired the messaging service and show us the line for that.

EDIT: Going over the code I believe your problem lies here

may you try print mouse.Hit.Position now

are we getting a number? because it says this number

109.02359, 0, 111.457291

OK, it seems you are getting a value perfect

change this line to this

local distance = (Vector3.new(targetPosition) - initalPos).Magnitude

ok so I think the code is good but my model isn’t one sec, let me try to fix it

if you want to know the error here it is

Enabled is not a valid member of Attachment “Workspace.Inferno9878.GrappleGun.Hook.HookWeld”

From what I can tell is that you are using an Attachment instead of a Weld please remove the instance and add the appropriate instance…

oh ok I will do that

one sec…

I am gonna reset the stuff real quick

ok almost got it

ok so now we have one more problem so below the distance variable or the one you wanted me to change there was a part where it sad

hook.Position = targetPosition

but that wouldn’t work, so what would I put there?

Sorry, that didn’t make sense at all to me, is there an error or is it just that script isn’t working properly?