Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error attempting to index a nil value?

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?

player = game.Players.LocalPlayer
character = player.Character
gun = character:FindFirstChild("Rifle")
mouse = player:GetMouse()
auto = false
function getModel(raw)
    for _, child in pairs(workspace:GetChildren()) do
    --calling pairs is barely any slower than using next directly, plus it's easier to read.
        if raw:IsDescendantOf(child) then
            return child;
        end;
    end;
    return nil; --this allows us to not use pcall
end;

mouse.Button1Down:connect(function()
	wait()
    auto = true
    while auto do
	wait()
    if mouse.Target then
	wait()
        local model = getModel(mouse.Target)
        if model and model:FindFirstChild("Humanoid") then
	wait()
            model.Humanoid.Health = model.Humanoid.Health - 15
            wait(0.2)
			game.ReplicatedStorage.gun.Value = game.ReplicatedStorage.gun.Value - 1
    end
mouse.Button1Up:connect(function()
    auto = false
end)
        end
    end
end)


3 comments

0

What line is this on?

0

9 and 23

0

9 and 24

2 answers

✓ Accepted answer

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

player = game.Players.LocalPlayer
character = player.Character
gun = character:FindFirstChild("Rifle")
mouse = player:GetMouse()
auto = false
function getModel(raw)
    for _, child in pairs(workspace:GetChildren()) do
    --calling pairs is barely any slower than using next directly, plus it's easier to read.
        if raw ~= nil and raw:IsDescendantOf(child) then
            return child;
        end;
    end;
    return nil; --this allows us to not use pcall
end;

mouse.Button1Down:connect(function()
    wait()
    auto = true
    while auto do
    wait()
    if mouse.Target then
    wait()
        local model = getModel(mouse.Target)
        if model and model:FindFirstChild("Humanoid") then
    wait()
            model.Humanoid.Health = model.Humanoid.Health - 15
            wait(0.2)
            game.ReplicatedStorage.gun.Value = game.ReplicatedStorage.gun.Value - 1
    end
mouse.Button1Up:connect(function()
    auto = false
end)
        end
    end
end)

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!

2 comments

0

it is a local script, and as i said lines 9 and 23 are causing the error

0

Yes, because line 9 and 23 are returning nil from the improper implementation of game.Players.LocalPlayer:GetMouse()

This discussion is preserved as it was. New answers, comments, and votes are closed.