Attempt to index nil with Button1Down?

So basically I have a ball which flings to the direction of where the player’s mouse clicked and since I can’t really do all this on the server I have to do it in StarterPlayerScripts in LocalScripts, there’s two teams Red and Blue, and the Blue team is able to throw the ball but the Red one gets the Attempt to index nil with Button1Down error and I iterally dont understand why, both of them have the same code (ofcourse with naming exceptions) but yet still I am able to fling the blue ball but my friend on red team isn’t able to do so…

The Code for Blue team:

local bps = ts["Blue"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.BlueBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")



evvB.OnClientEvent:Connect(function()
	for i, plr in pairs (bps) do
		local mouse = plr:GetMouse()
	mouse.Button1Down:Connect(function()
			if not deb then	
				deb = true
				local g = Vector3.new(0, -game.Workspace.Gravity, 0);
				local x0 = ball.CFrame * Vector3.new(0, 0, 0)
				local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
				local c = ball:Clone();
				c.Name = "BlueBallC"
				c.Velocity = v0;
				--	c.CFrame = CFrame.new(x0);
				c.CanCollide = true;
				c.Anchored = false;
				print("blue ball created")
				c.Parent = game.Workspace;
			end
		wait(2)
		deb = false
	end)
end
end)

Code For Red Team:

local ts = game:GetService("Teams");
local rps = ts["Red"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.RedBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")



evvR.OnClientEvent:Connect(function()
   for i, plr in pairs (rps) do
   	local mouse = plr:GetMouse()
   	mouse.Button1Down:Connect(function()
   		if not deb then	
   			deb = true
   			local g = Vector3.new(0, -game.Workspace.Gravity, 0);
   			local x0 = ball.CFrame * Vector3.new(0, 0, 0)
   			local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
   			local c = ball:Clone();
   			c.Name = "RedBallC"
   			c.Velocity = v0;
   			--	c.CFrame = CFrame.new(x0);
   			c.CanCollide = true;
   			c.Anchored = false;
   			print("red ball created")
   			c.Parent = game.Workspace;
   		end
   		--end
   		wait(2)
   		deb = false
   	end)
   end
end)

Just FYI, the OnClientEvent is fired to all clients from the server when an Integer Value on the workspace is either 1 or 2 which depicts the red team’s turn or blue team’s turn.

Earlier I had a different code which worked for blue team only as well but I recognized the issue in both the scripts then which was that I had used local players like game.Players.LocalPlayer, so it Picked one player who probably joined the game first and then thought that was the player throught both scripts. So this was the reason I had to use that for loop to go through player in the specific team.

u cant get other players mouse from local script

What exactly do you mean by “other players”? Because I was very much able to get moue int he blue team script and was able to fling the ball too

From my experience with this kind of error, it means that the instance you are trying to pass a property of is equal to nil. In this case the game sees “mouse” as nil. what’s in the table that you are looping through to get the player? That might be the what’s causing the issue.

The table rps just contains players in the red team

Technically you can. If you loop through the players and then use getMouse() like he did then you could get the mouse of other players. I haven’t tested it but I think it’s possible

Can I see the code snippet?

I sent both the code snippets in the topic, are you not able to view them? Still I will send you it

local ts = game:GetService("Teams");
local rps = ts["Red"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.RedBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")



evvR.OnClientEvent:Connect(function()
	for i, plr in pairs (rps) do
		local mouse = plr:GetMouse()
		mouse.Button1Down:Connect(function()
			if not deb then	
				deb = true
				local g = Vector3.new(0, -game.Workspace.Gravity, 0);
				local x0 = ball.CFrame * Vector3.new(0, 0, 0)
				local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
				local c = ball:Clone();
				c.Name = "RedBallC"
				c.Velocity = v0;
				--	c.CFrame = CFrame.new(x0);
				c.CanCollide = true;
				c.Anchored = false;
				print("red ball created")
				c.Parent = game.Workspace;
			end
			--end
			wait(2)
			deb = false
		end)
	end
end)

Ah my bad I didn’t see that you posted the variables as well

before calling plr:GetMouse() try adding this

print(plr)

If it prints nil then that’s your problem

So I put that in the blue one, and it printed my name now I will need to put it in the red one as well and test it with my friend once

1 Like

If the evvB OnClientEvent is fired from server to all clients, then you won’t need to loop through all players to get mouse but you can instead use the current client (since LocalScripts are given to all clients).

Try this

Blue Team:

local bps = ts["Blue"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.BlueBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")
local player = game.Players.LocalPlayer

evvB.OnClientEvent:Connect(function()
	if player.Team == ts["Blue"] then
		local mouse = player:GetMouse()
		
		mouse.Button1Down:Connect(function()
			if not deb then	
				deb = true
				local g = Vector3.new(0, -game.Workspace.Gravity, 0);
				local x0 = ball.CFrame * Vector3.new(0, 0, 0)
				local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
				local c = ball:Clone();
				c.Name = "BlueBallC"
				c.Velocity = v0;
				--	c.CFrame = CFrame.new(x0);
				c.CanCollide = true;
				c.Anchored = false;
				print("blue ball created")
				c.Parent = game.Workspace;
			end
			wait(2)
			deb = false
		end)
	end
end)

Red Team:

local ts = game:GetService("Teams");
local rps = ts["Red"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.RedBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")
local player = game.Players.LocalPlayer

evvR.OnClientEvent:Connect(function()
	if player.Team == ts["Blue"] then
		local mouse = player:GetMouse()
   	
		mouse.Button1Down:Connect(function()
   			if not deb then	
   				deb = true
   				local g = Vector3.new(0, -game.Workspace.Gravity, 0);
   				local x0 = ball.CFrame * Vector3.new(0, 0, 0)
   				local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
   				local c = ball:Clone();
   				c.Name = "RedBallC"
   				c.Velocity = v0;
   			--	c.CFrame = CFrame.new(x0);
   				c.CanCollide = true;
   				c.Anchored = false;
   				print("red ball created")
   				c.Parent = game.Workspace;
   			end
   			--end
   			wait(2)
   			deb = false
		end)
	end
end)
1 Like

Yes that could work.

I did it but for some reason it is identifying the local player on red and blue script as me only, and not my friend at all
image

Can you show us the server firing to the clients script?

Yes sure also dont be concerned about the last few lines they just change the turn IntValue on the workspace

local rs = game:GetService("ReplicatedStorage")
local ingressevent = rs:WaitForChild("ingress")
local ss = game:GetService("ServerStorage")
local ev1 = rs:WaitForChild("CamBlu")
local evB = rs:WaitForChild("TurnBlu")
local evR = rs:WaitForChild("TurnRed")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")
local ts = game:GetService("Teams")

game.Workspace.Turn.Changed:Connect(function(val)
	print(val)
	ev1:FireAllClients(val)
    if val == 1 then
    evvB:FireAllClients()
    elseif val == 2 then
    evvR:FireAllClients()
    end
	end)

game.Players.PlayerAdded:Connect(function(player)
	local plars = game.Players:GetPlayers()
	if #plars == 2 then
		print("game started")
		game.Workspace.GameIngress.Value = true
		game.Workspace.Turn.Value = 1
		local pongs = ss:FindFirstChild("Pong"):Clone()
		local detecsB = ss:FindFirstChild("Detectors"):Clone()
		local detecsR = ss:FindFirstChild("DetectorsRed"):Clone()
		pongs.Parent = game.Workspace
        detecsB.Parent = game.Workspace
		detecsR.Parent = game.Workspace
		wait(1)
		ingressevent:FireAllClients(game.Workspace.GameIngress.Value, game.Workspace.Turn.Value)
   end
end)

evB.OnServerEvent:Connect(function()
	game.Workspace.Turn.Value = 1
	
end)

evR.OnServerEvent:Connect(function()
	game.Workspace.Turn.Value = 2
end)

WHOOPS, I have a typo in my script, try this (I was accidentally checking if player was in blue team for the red event)

Blue Team:

local bps = ts["Blue"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.BlueBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")
local player = game.Players.LocalPlayer

evvB.OnClientEvent:Connect(function()
	if player.Team == ts["Blue"] then
		local mouse = player:GetMouse()
		
		mouse.Button1Down:Connect(function()
			if not deb then	
				deb = true
				local g = Vector3.new(0, -game.Workspace.Gravity, 0);
				local x0 = ball.CFrame * Vector3.new(0, 0, 0)
				local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
				local c = ball:Clone();
				c.Name = "BlueBallC"
				c.Velocity = v0;
				--	c.CFrame = CFrame.new(x0);
				c.CanCollide = true;
				c.Anchored = false;
				print("blue ball created")
				c.Parent = game.Workspace;
			end
			wait(2)
			deb = false
		end)
	end
end)

Red Team:

local ts = game:GetService("Teams");
local rps = ts["Red"]:GetPlayers()
local t = 1;
local ball = game.Workspace.Balls.RedBall;
local deb = false
local rs = game:GetService("ReplicatedStorage")
local evvB = rs:WaitForChild("MotionBlu")
local evvR = rs:WaitForChild("MotionRed")
local player = game.Players.LocalPlayer

evvR.OnClientEvent:Connect(function()
	if player.Team == ts["Red"] then
		local mouse = player:GetMouse()
   	
		mouse.Button1Down:Connect(function()
   			if not deb then	
   				deb = true
   				local g = Vector3.new(0, -game.Workspace.Gravity, 0);
   				local x0 = ball.CFrame * Vector3.new(0, 0, 0)
   				local v0 = (mouse.Hit.p - x0 - 0.25*g*t*t)/t;
   				local c = ball:Clone();
   				c.Name = "RedBallC"
   				c.Velocity = v0;
   			--	c.CFrame = CFrame.new(x0);
   				c.CanCollide = true;
   				c.Anchored = false;
   				print("red ball created")
   				c.Parent = game.Workspace;
   			end
   			--end
   			wait(2)
   			deb = false
		end)
	end
end)

Yes I changed that but that isnt going to do anything though since it is recognising the localplayer as me only for some reason, maybe the person who joins the game first only for some reason…

Every client is its own localplayer with their own localscripts, are you sure you changed

evvR.OnClientEvent:Connect(function()
	if player.Team == ts["Blue"] then

to

evvR.OnClientEvent:Connect(function()
	if player.Team == ts["Red"] then

?

Alright I will give it a test, yes now I have it changed, I will tell you the result of it