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

ServerScriptService.Script:2: attempt to index nil with 'GetMouse' bug?

How to do I fix this bug in my code of "ServerScriptService.Script:2: attempt to index nil with 'GetMouse'" in Line 2?

 local t = 1;
 local mouse = game.Players.LocalPlayer:GetMouse();
 local hrp = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart");
 local bball = game.ServerStorage.PracticeBall
 local rs = game:GetService("RunService").RenderStepped;

 mouse.Button1Down:Connect(function()
     local g = Vector3.new(0, -game.Workspace.Gravity, 0);
     local x0 = hrp.CFrame * Vector3.new(0, 2, -2)

     -- calculate the v0 needed to reach mouse.Hit.p
     local v0 = (mouse.Hit.p - x0 - 0.5*g*t*t)/t;

     local c = bball:Clone();
     c.CanCollide = true;
     c.Anchored = true;
     c.Parent = game.Workspace;

     -- have the ball travel that path
     local nt = 0;
     while (nt < t*2) do
          c.CFrame = CFrame.new(0.5*g*nt*nt + v0*nt + x0); 
              nt = nt + rs:Wait();
     end
 end)

2 comments

0

Is this a server script? It's the only case I can think of where LocalPlayer is nil.

0

It is.

1 answer

✓ Accepted answer

The LocalPlayer only exists on local scripts; normal scripts do not have an associated player and have no mouse to get.

4 comments

0

Finally, someone who understands why the pointer returns nil :,,)

0

Thank you. Where should I place my LocalScript with my code in?

0

Well, I'm not 100% sure but I'd say StarterCharacterScripts. But remember that parts cloned in a local script do not exist on the server! You'll need a remote event for that.

0

Okay.

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