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

Even after fixes and changes my DataStore script has a weird error, help?

local DataStore = game:GetService("DataStoreService"):GetDataStore("LastLogin")
player = game.Players.LocalPlayer
playergui = game.Players.LocalPlayer.PlayerGui.RewardGUI


game.Players.PlayerAdded:connect(function(Player)

	local LastLogin = DataStore:GetAsync(Player.userId)
	local leaderstats = player:WaitForChild("leaderstats")

	if LastLogin then

		local Seconds = tick()-LastLogin
		local Minutes = Seconds/60
		local Hours = Minutes/60
		playergui.TimeFrame.TextLabel.Text = ("Next Award In: "..Hours.."")

		if Hours >= 24 then

			playergui.NotificationFrame.Visible = true	
			
			for SoundNum = 1, 7 do
				script.Parent["CointDrop"..SoundNum]:Play()
				wait(.1)
			end
	 		
	 		leaderstats.Coins.Value = leaderstats.Coins.Value +100

   			wait(2)
			playergui.NotificationFrame.Visible = false
			DataStore:SetAsync(Player.userId,tick())

		end

	else

		DataStore:SetAsync(Player.userId,tick())

	end

end)

The output is telling me: Workspace.Script:3: attempt to index field 'LocalPlayer' (a nil value)

I don't know what this means and what i'm supposed to fix :/

1 answer

Ok.. LocalPlayer will only work in a LocalScript. Data Stores will not work on a LocalScript. Therefore, you need to define `playergui' after the event because only then we have a defined player from which we can work from - we can then delete line 2 as well.

Secondly, 'if LastLogin then' will not work because the GetAsync() may not return a Boolean. In this case, you may want to check if it's not nil (or a specific value)

local DataStore = game:GetService("DataStoreService"):GetDataStore("LastLogin")

game.Players.PlayerAdded:connect(function(Player) --Here's the player that entered
    playergui = Player.PlayerGui.RewardGUI --Get the GUI from the player that entered
    local LastLogin = DataStore:GetAsync(Player.userId)
    local leaderstats = player:WaitForChild("leaderstats")

    if LastLogin ~= nil  then --If GetAsync() Does not return nil (i.e something was saved)

        local Seconds = tick()-LastLogin
        local Minutes = Seconds/60
        local Hours = Minutes/60
        playergui.TimeFrame.TextLabel.Text = ("Next Award In: "..Hours.."")

        if Hours >= 24 then

            playergui.NotificationFrame.Visible = true  

            for SoundNum = 1, 7 do
                script.Parent["CointDrop"..SoundNum]:Play()
                wait(.1)
            end

            leaderstats.Coins.Value = leaderstats.Coins.Value +100

            wait(2)
            playergui.NotificationFrame.Visible = false
            DataStore:SetAsync(Player.userId,tick())

        end

    else

        DataStore:SetAsync(Player.userId,tick())

    end

end)

If my answer helped you, please do check this as the correct answer.

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