Attempt to index nil with Button1Down?


Okay So I tested it, it doesnt work the red team guy is not able to shoot ball. However it does print his name, since I put a print(plr) and on my dev console it prints my name

Just tested your issue using this script:

for _, player in pairs(game.Players:GetPlayers()) do 
	print(player, player:GetMouse())
end

The results I got for Player1 where:

Player1 Instance  -  Client - LocalScript:2
Player2 nil  -  Client - LocalScript:2

Therefore it’s impossible to get a user mouse from another LocalScript or a Script. The issue can easily be fixed by using remotes:

--client, LocalScript
local Remote = game.ReplicatedStorage.Remote --remote path

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
	local hit = mouse.Hit 
	if hit then 
		Remote:FireServer(hit.p)
	end 
end)
--server, Script
local Remote = game.ReplicatedStorage.Remote --remote path

--debounces
local deb = {}

Remote.OnServerEvent:Connect(function(player, p)
	if table.find(deb, player) then return end 
	table.insert(deb, player)
	
	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 = workspace
	
	task.wait(2)
	local index = table.find(deb, player)
	if index then 
		table.remove(deb, index)
	end
end)
1 Like

Ah wait, the ball creation is in a local script meaning that the ball will only be visible to the current client (a blue player will only see their own ball, a red player will only see their own ball). Try creating the ball in the server by doing :FireServer on the client when the mouse.Button1Down event is running.

Yes this thing definetely worked! However I am now having an issue that for some reason the function PlayerAdded is not firing at all, I put prints in it to check if it fires at all but it doesn’t this is so confusing how can this even be possible? Btw I transfered all that mainc lient code I had onto the server seeing what you told me to do.

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 infB = rs:WaitForChild("InfoB")
local infR = rs:WaitForChild("InfoR")
local ts = game:GetService("Teams")
local t = 1;
local ball = game.Workspace.Balls.BlueBall;
local turn = game.Workspace.Turn
local detecs = game.Workspace:WaitForChild("Detectors")
local detecsR = game.Workspace:WaitForChild("DetectorsRed")
local bc = game.Workspace.Pong["Blue cups"]
local rc = game.Workspace.Pong["Red cups"]

local deb = {}


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


game.Players.PlayerAdded:Connect(function(player)
	local plars = game.Players:GetPlayers()
	print("function test?")
	if #plars == 1 then
		print("game started")
		game.Workspace.GameIngress.Value = true
		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)



--Motions

evvB.OnServerEvent:Connect(function(player, p)
	if table.find(deb, player) then return end 
	table.insert(deb, player)
	local g = Vector3.new(0, -game.Workspace.Gravity, 0)
	local x0 = ball.CFrame * Vector3.new(0, 0, 0)
	local v0 = (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
	c.Parent = workspace
	task.wait(2)
	local index = table.find(deb, player)
	if index then 
		table.remove(deb, index)
	end
end)

evvR.OnServerEvent:Connect(function(player, p)
	if table.find(deb, player) then return end 
	table.insert(deb, player)
	local g = Vector3.new(0, -game.Workspace.Gravity, 0)
	local x0 = ball.CFrame * Vector3.new(0, 0, 0)
	local v0 = (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
	c.Parent = workspace
	task.wait(2)
	local index = table.find(deb, player)
	if index then 
		table.remove(deb, index)
	end
end)

-- When Ball touches surface
game.Workspace.Table.Surface.Touched:Connect(function(hit)
	if turn.Value == 1 then
		if hit.Name == "BlueBallC" then
			print("Haha blue you missed")
			turn.Value = 2
		end
	end
end)

game.Workspace.Table.Surface.Touched:Connect(function(hit)
	if turn.Value == 2 then
		if hit.Name == "RedBallC" then
			print("Haha red you missed")
			turn.Value = 1
		end
	end
end)```

Actually nvm I saw it was because of the WaitForChild I then used ChildAdded to fix it

you cant get other’s mouse object even if the player is a team with you