Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Error attempting to index a nil value?

Asked by 8 years ago

The script is suppose to allow the player to simulate "shooting" and when there cursor goes over a player, they will take damage. the issue is, after a while of "shooting" i get this error attempt to index local 'raw' (a nil value)

Any solutions?

01player = game.Players.LocalPlayer
02character = player.Character
03gun = character:FindFirstChild("Rifle")
04mouse = player:GetMouse()
05auto = false
06function getModel(raw)
07    for _, child in pairs(workspace:GetChildren()) do
08    --calling pairs is barely any slower than using next directly, plus it's easier to read.
09        if raw:IsDescendantOf(child) then
10            return child;
11        end;
12    end;
13    return nil; --this allows us to not use pcall
14end;
15 
View all 35 lines...
0
What line is this on? Monsieur_Robert 338 — 8y
0
9 and 23 pluginfactory 463 — 8y
0
9 and 24 pluginfactory 463 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Your problem is that when mouse.Target is the skybox, it returns nil. The simple fix is checking to make sure it isn't nil before carrying out with the code

Check line 9 to see what I've changed

01player = game.Players.LocalPlayer
02character = player.Character
03gun = character:FindFirstChild("Rifle")
04mouse = player:GetMouse()
05auto = false
06function getModel(raw)
07    for _, child in pairs(workspace:GetChildren()) do
08    --calling pairs is barely any slower than using next directly, plus it's easier to read.
09        if raw ~= nil and raw:IsDescendantOf(child) then
10            return child;
11        end;
12    end;
13    return nil; --this allows us to not use pcall
14end;
15 
View all 35 lines...
Ad
Log in to vote
0
Answered by 8 years ago

Well, as seen in your question, you're indexing a nil value. The only thing I can think of making game.Players.LocalPlayer:GetMouse().Target return nil is this script's implementation.

This script should be ran from a LocalScript, since LocalScripts are the only class that can invoke game.Players.LocalPlayer and have it work correctly throughout servers.

Hope this helps! Comment if you need more assistance or a further explanation!

0
it is a local script, and as i said lines 9 and 23 are causing the error pluginfactory 463 — 8y
0
Yes, because line 9 and 23 are returning nil from the improper implementation of game.Players.LocalPlayer:GetMouse() TheEdgyDev 50 — 8y

Answer this question