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)
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
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
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
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