0

# Attempt to index nil with lookVector?

I am making a magic system in my RPG. The original script works very well on PC and I wanted to add mobile support. I got that bit sorted but on mobile I get the error in the title. I cannot find an alternative, I tried using UnitRay but that didn't work either. Any help is appreciated.

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService('Debris')

--// Variables \\--
local Remote = ReplicatedStorage.FireBallEvent
local FireBall = ReplicatedStorage.FireBall
local player = game.Workspace:FindFirstChild(game.Workspace.PlayerName.Value)

--// Settings \\--
local Damage = 20

local ServerDebounces = {}

Remote.OnServerEvent:Connect(function(plr, Mouse)
if not ServerDebounces[plr] then
ServerDebounces[plr] = true

local Char = plr.Character or plr.CharacterAdded:Wait()

local FireBallClone = FireBall:Clone()
FireBallClone.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,-3)
FireBallClone.Parent = workspace

local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = Mouse.lookVector ----- This is the problem line
BodyVelocity.Parent = FireBallClone

spawn(function()
for i = 0, 1000 do
wait()
FireBallClone.Size = FireBallClone.Size + Vector3.new(0.07,0.07,0.07)
BodyVelocity.Velocity = Mouse.lookVector*i
end
end)
ServerDebounces[plr] = false

local Debounce = true
FireBallClone.Touched:Connect(function(h)
if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Debounce then
Debounce = false
local Enemy = h.Parent.Humanoid
Enemy:TakeDamage(Damage)
FireBallClone.Transparency = 1
BodyVelocity:Destroy()
wait(1)
FireBallClone:Destroy()
wait(3)
Debounce = true
end
end)
end
end)


1
TGazza 799
7 months ago
Edited 7 months ago

Edited with working code!

Local Script:

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--// Variables \\--
local Remote = ReplicatedStorage.FireBallEvent
local PlayerScripts = script.Parent
local Player = PlayerScripts.Parent
local Mouse = Player:GetMouse()
local Char = Player.Character or Player.CharacterAdded:Wait()

local Debounce = false
Mouse.Button1Down:Connect(function()

local CharRP = Char.HumanoidRootPart
if(Debounce == false) then
Debounce = true
Remote:FireServer((Mouse.Hit.Position - CharRP.Position).unit)<-- pass to the server the mockup MouseLookvetor
Debounce  = false
end
end)


Server Side:

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService('Debris')

--// Variables \\--
local Remote = ReplicatedStorage.FireBallEvent
local FireBall = ReplicatedStorage.FireBall
local player = game.Workspace:FindFirstChild(game.Workspace.PlayerName.Value)

--// Settings \\--
local Damage = 20

local ServerDebounces = {}

Remote.OnServerEvent:Connect(function(plr,MouseLV)
if not ServerDebounces[plr] then
ServerDebounces[plr] = true
local Char = plr.Character or plr.CharacterAdded:Wait()

local FireBallClone = FireBall:Clone()
FireBallClone.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,-3)
FireBallClone.Parent = workspace

local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = MouseLV ----- This is the problem line
BodyVelocity.Parent = FireBallClone

spawn(function()
for i = 0, 1000 do
wait()
FireBallClone.Size = FireBallClone.Size + Vector3.new(0.07,0.07,0.07)
BodyVelocity.Velocity = MouseLV*i
end
end)
ServerDebounces[plr] = false

local Debounce = true
FireBallClone.Touched:Connect(function(h)
if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Debounce then
Debounce = false
local Enemy = h.Parent.Humanoid
Enemy:TakeDamage(Damage)
FireBallClone.Transparency = 1
BodyVelocity:Destroy()
wait(1)
FireBallClone:Destroy()
wait(3)
Debounce = true
end
end)
end
end)



as you can see i've changed the code a little bit as passing the Mouse object to the server didnt work, BUT sending the mockup Lookvector does! thats what this example does. It incorporates the HumanoidRootPart.Position and the Mouse.Hit.Position from the local script so it keeps the server script happy. I haven't fully tested the code but it works as far as firing the fireball from your chest area and it slowly moves away then speeds up.

I think the problem you ran into is the ServerEvent object allways returns the player who fired it and the server cannot use the Mouse object from the clients/players. But we can make a workaround as i've done in the scripts above by passing the lookvector as a Vector3 Value.

Might need to refine when defining the players root part on the server side script by passing the character as a varable or something.

Hope this helps!

p.s looks like a cool idea you have going on here! :D

0
I get: attempt to index nil with 'Hit' iiPizzaCraver 72 — 7mo
0
and on PC it says: Hit is not a valid member of CFrame iiPizzaCraver 72 — 7mo
0
Btw thank you so much for your Guardian Angel Defender plugin, I really appreciate it! You've helped me lots with it iiPizzaCraver 72 — 7mo
0
Kinda random lol but i see the error in my code just change local Mouse_lookVector = (CharRP.Position - Mouse.Hit).unit to local Mouse_lookVector = (CharRP.Position - Mouse.Hit.Position).unit and it should work unless your script is not hooking up your mouse TGazza 799 — 7mo
0
I still get the same errors as before iiPizzaCraver 72 — 7mo
0
I tried getting PlayerMouse in the local script but it also says that lookVector is not a valid member of PlayerMouse iiPizzaCraver 72 — 7mo
0
hmm ill see if i can make a make a local script that uses the mouse hit and the centre point of the players character then use that vector as a direction creating a "lookVector". Ill post a link back here when its done TGazza 799 — 7mo
0
Hmm.. just a thought ... just before your error the Mouse.LookVector bit try printing out what Mouse value actually is. Something like print(Mouse) or even print(typeof(Mouse)) so we know what we're dealing with. TGazza 799 — 7mo
0
Got a little carried away with an example lol. Heres my script. https://bit.ly/2EaRxZa its a mockup of a wand script that shoots out yellow magic and it explodes on contact. The example is using that Mouse.hit Cframe as its base for the mouse direction. Hope this helps ya! TGazza 799 — 7mo
0
Thank you, I got the local script working very well. However, in the server script, it says "attempt to index nil with 'Hit'" iiPizzaCraver 72 — 7mo
0
sounds like the server isnt seeing the Mouse object of the player.... Ill do a mockup of your server script and ill see if i can get it working with the code i provided for the client-local script. Is there anything else required to get the server script running? (other than the varables linked up in your script)?, If i get it working ill re-post what i have as an answer overriding my original. TGazza 799 — 7mo
0
Assuming i can lol TGazza 799 — 7mo
0
your server and a mockup client scripts should be on this answer as i've changed it completely with the working code examples! TGazza 799 — 7mo
0
Thank you very much! Got it working on mobile, all I have now is a PC error where it goes "invalid argument #3 (Vector3 expected, got CFrame) iiPizzaCraver 72 — 7mo
0
At line 26 iiPizzaCraver 72 — 7mo
0
CFrame is composed of 2 vectors, 1 for position of the part and 1 for it's rotation CFrame.p turns the CFrame into a vector3 by only taking it's position values, the second vector is mouse.hit by combining them you get a new CFrame. From what I understand, we might need to change the values of MouseLV into a position? iiPizzaCraver 72 — 7mo
0
I attempted to print out whatever MouseLV's value is and it's this:   -9162.2207, 1422.07751, -1643.17358, 0.156244457, 0.132078186, 0.978847802, 5.69522381e-05, 0.991017878, -0.133729428, -0.987718403, 0.0209502354, 0.154833555 so I can see why the script is confused iiPizzaCraver 72 — 7mo
0
And Im now getting the same error on both devices again lol. Everything else in the script appears to work very well though and I've connected my mobile script to the Mana bar successfully. iiPizzaCraver 72 — 7mo
0
hmm.. are you on discord. or maybe robloxes team create thingy.if you like i could go over what you have and fix the problems.On the second note where you're getting the cframe errors just cast them to a vector3 by Vector3.new(CFrame). though it sounds like you may have a few loose ends in your client scripts. TGazza 799 — 7mo
0
Yeah I have discord, my alt user is: Lomando#7544 iiPizzaCraver 72 — 7mo
0
ok just sent you a friend request. im TGazza#8749 TGazza 799 — 7mo
0
It’s accepted iiPizzaCraver 72 — 7mo
0

You’re trying to send the player’s mouse, an object that only exists on the client, to the server, meaning it will come up as nil.

0
Sooo how would that look in the script? iiPizzaCraver 72 — 7mo
0

In the LocalScript sided, where you made the :FireServer(), please replace it to this:

local Mouse = game.Players.LocalPlayer:GetMouse()
game.ReplicatedStorage.FireBallEvent:FireServer(Mouse.lookVector)


And then replace in line 27 of the server script to:

BodyVelocity.Velocity = Mouse


So you got the lookVector via localscript, and you won't get the lookVector in server script, because mouse is only seen from client.

0
I get: ContextActionService: Unexpected error while invoking callback: lookVector is not a valid member of PlayerMouse iiPizzaCraver 72 — 7mo
0
Ah I see why, there is no lookVector in Mouse. Gabe_elvin1226aclan 323 — 7mo
0
So what do I use instead? iiPizzaCraver 72 — 7mo
0
I'm not sure but you can use Mouse.X which is right of the mouse or Mouse.Y which is above Gabe_elvin1226aclan 323 — 7mo
0
ugh thats not going to solve anything iiPizzaCraver 72 — 7mo
0
its LookVector btw zadobyte 692 — 7mo
0
Thank you for that irrelevant information iiPizzaCraver 72 — 7mo
0
There's no LookVector in Mouse Gabe_elvin1226aclan 323 — 7mo
0
SO WHAT DO I USE INSTEAD iiPizzaCraver 72 — 7mo
0
IDK IM NOT SURE... Gabe_elvin1226aclan 323 — 7mo
0
Well why are you here then iiPizzaCraver 72 — 7mo