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

Attempted to index nil with :GetMouse()??

I am using a script in starter pack.

While im typing, it says I can use LocalPlayer and :GetMouse()

Help? Code:

game.Players.PlayerAdded:Connect(function(plr)
local mouse = plr:GetMouse()

local mouseDistance = 30

local distance = nil

local Mouse1Down = false
local Mouse2Down = false

local function GetMaxDistance()
	distance = (player.Character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
	-- print(distance)
end

function DestroyBlock()
	GetMaxDistance()
	if mouse.Target.Name == "log" and distance <= mouseDistance then
		local logStregth = 5
		local setBlock = mouse.Target
		for i = 0,logStregth,1 do
			if Mouse2Down == false then
				print("Mouse went up")
				break
			end
			if i == logStregth then
				setBlock:Destroy()
				if player.Items:FindFirstChild("Log") then
					player.Items.Log.Value += 1
				else
					local newLogItem = Instance.new("NumberValue", player.Items)
					newLogItem.Name = "Log"
					newLogItem.Value = 1
				end
			end
			wait(1)
		end
	end
end
function PlaceBlock()
	GetMaxDistance()
	if distance <= mouseDistance then
		local newBlock = game.ReplicatedStorage.Assets.grass:Clone()
		newBlock.CFrame = CFrame.new(mouse.Target.Position + Vector3.new(0,4,0))
		newBlock.Parent = mouse.Target.Parent
	end
end

mouse.Button1Down:Connect(PlaceBlock)
mouse.Button2Down:Connect(DestroyBlock)

mouse.Button1Down:Connect(function()
	Mouse1Down = true
end)

mouse.Button2Down:Connect(function()
	Mouse2Down = true
end)

mouse.Button1Up:Connect(function()
	Mouse1Down = false
end)

mouse.Button2Up:Connect(function()
	Mouse2Down = false
end)

yes, these 2 lines of code aint working IT IS A SCRIPT, NOT A LOCAL OR MOUDLE

1 comment

0

:GetMouse() only works in LocalScript

2 answers

To access both UIS and Mouse you must use a Local script (client sided script). This is because it only happens on the client and does not replicate to the server. If you want to bypass the client -> server boundary please have a look into RemoteEvents. Like this:

-- local script
game.ReplicatedStorage["EVENT HERE"]:FireServer(a, b);
-- server script
game.ReplicatedStorage["EVENT HERE"].OnServerEvent:Connect(function(plr, a, b)
       -- // code;
end)

either it needs to be local for it to work or roblox lua has changed to the point that code is out dated so look into UserInputService you can get the api referance here https://developer.roblox.com/en-us/api-reference/function/UserInputService/GetKeysPressed

2 comments

0

I believe it has to be a local script for :GetMouse() to function correctly.

0

if it were outdated they wouldn't outright delete it

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