Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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)
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 greatneil80 2647 — 3y

1 answer

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

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

Ad

Answer this question