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

Workspace.Checkpoints.2.Time:2: attempt to index nil with 'GetMouse' Error??

Hi there. Newbie coder here. So I was writing this code, which is supposed to be very simple. If you touch the script.Parent (A part), a timer is called from the starter Gui and is supposed to become visible. The timer counts down from 40 and when it reaches 0, the player dies (resets sorta). Every time I run the script, I get this error, Workspace.Checkpoints.2.Time:2: attempt to index nil with 'GetMouse'. I don't know why. Please help!!

player = game.Players.LocalPlayer
mouse = player:GetMouse()
char = player.Character
h = char:WaitForChild("Humanoid")
seconds = 40
timer = game.StarterGui.Stopwatch.Timer

script.Parent.Touched:Connect(function()
	timer.Visible = true
	while seconds ~= 0 do
		seconds = seconds-1
		wait(1)
		timer.Text = (seconds.." seconds")
		if seconds == 0 then
			h.Health = 0
		end
	end
end)

1 comment

0

If this is a regular script then LocalPlayer won't work. Since the server isn't a person... Also, game.StarterGui will work but not throw an error, you just won't see it. Use the PlayerGui or index directly from startergui using script.Parent

1 answer

I see 3 errors.

  1. You used startergui instead of localplayer.PlayerGui. That will not work at all.

  2. It is likely a normal script (and it should be one). You cannot use localplayer in scripts. They can only be used in local scripts.

  3. You never actually needed to use :GetMouse(). It is deprecated (Roblox don't update it and is replaced by "UserInputService") anyways. Here is a fixed version of your script.

local seconds = 40

script.Parent.Touched:Connect(function(hit) -- hit is the Touched function's parameter. it determines the instance/object that touched "script.Parent"
	local Players = game:GetService("Players") -- getting the "Players" service.
	local player = Players:GetPlayerFromCharacter(hit.Parent) -- get the player by an instance (returns nil if no player)
	if player ~= nil then -- if player exists then
		local chr = hit.Parent -- since by using get player from character we confirmed hit.Parent is the character, we can safely use this as a variable
		local timer = player.PlayerGui.StopWatch.Timer -- playerGui instead of StarterGui.
		local h = chr:FindFirstChildWhichIsA("Humanoid")
		timer.Visible = true
		while true do -- am smol hed so I use while true loop instead lmao
			seconds -= 1 -- ye you can do that
			timer.Text = tostring(seconds).." seconds"
			if seconds == 0 then
				h.Health = 0
				break -- this will stop the loop
			end
		end
	end
end)

Links to know the stuff I used in this script.

Players:GetPlayerFromCharacter()

Loops

Touched Event

some of these you already know but have extra info which you might not know about said things

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