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)
I see 3 errors.
You used startergui instead of localplayer.PlayerGui. That will not work at all.
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.
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()
some of these you already know but have extra info which you might not know about said things