PlayerAdded triggers multiple times for the same player

Hello Devforum people

I encountered pretty obscure issue with my daily log in bonus system for Premium users.
Basically, in my game whenever player has premium they get daily currency and x2 income bonus, but for some unknown reason when we were doing testings with my friend, we were joining approximatelly at the same time, and I was getting all of the reward. Here’s the scripts:

  1. ServerScriptService script that handles the Events:
local DataStoreService = game:GetService("DataStoreService")
local DailyRewardDataStore = DataStoreService:GetDataStore("PoopyHead")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		if player.MembershipType ~= Enum.MembershipType.Premium then -- set to not equal to for testing purposes

			-- Daily Reward
			local foundData = DailyRewardDataStore:GetAsync(player.UserId)

			if foundData then
				if tonumber(foundData) < os.time() - 60 then
					print(player.Name.." is receiving their daily reward")

					player.PlayerGui.RobloxPremiumReward.Enabled = true -- setting the reward UI visable
					DailyRewardDataStore:SetAsync(player.UserId, os.time())
				else
					print("Player "..player.Name.." did not receive the rewards, here's how much time they have left: "..foundData - os.time())
				end
			else
				DailyRewardDataStore:SetAsync(player.UserId, os.time())
			end

			-- x2 Income Bonus
			player.leaderstats.Minutes.valname.Value = player.leaderstats.Minutes.valname.Value * 2
			player.stats.DefaultMinutes.valname.Value = player.stats.DefaultMinutes.valname.Value * 2
		end
	end)
end)
  1. Daily reward UI script:
local event = game.ReplicatedStorage.EventFolder.GetPlayyer -- just an event to get a player from local script

event.OnServerEvent:Connect(function(player)
	script.Parent.Parent.Parent.Enabled = false
	player.leaderstats.Minutes.Value = player.leaderstats.Minutes.Value + 10
end)

What’s even more obscure is the fact that x2 income bonus works half of the time… for my friend, it works all the time for me, just not for him. I honestly have no idea what causes that.

I have a suspicion that maybe roblox gets confused in the UI script part, since clicking the button fires the event of a player, which is ok, but why does x2 income bonus doesnt work???

local DataStoreService = game:GetService("DataStoreService")
local DailyRewardDataStore = DataStoreService:GetDataStore("PoopyHead")

game.Players.PlayerAdded:Connect(function(player)
    local success, error = pcall(function()
        if player.MembershipType ~= Enum.MembershipType.Premium then

            -- Daily Reward
            local foundData = DailyRewardDataStore:GetAsync(player.UserId)

            if foundData then
                local lastLoginDate = os.date("*t", foundData)
                local currentDate = os.date("*t", os.time())

                if lastLoginDate.yday < currentDate.yday then
                    print(player.Name.." is receiving their daily reward")

                    player.PlayerGui.RobloxPremiumReward.Enabled = true
                    DailyRewardDataStore:SetAsync(player.UserId, os.time())
                else
                    print("Player "..player.Name.." did not receive the rewards, here's how much time they have left: "..foundData - os.time())
                end
            else
                DailyRewardDataStore:SetAsync(player.UserId, os.time())
            end

            -- x2 Income Bonus
            if player.leaderstats.Minutes.valname and player.stats.DefaultMinutes.valname then
                player.leaderstats.Minutes.valname.Value = player.leaderstats.Minutes.valname.Value * 2
                player.stats.DefaultMinutes.valname.Value = player.stats.DefaultMinutes.valname.Value * 2
            else
                print("Missing required values for x2 Income Bonus")
            end
        end
    end)

    if not success then
        warn("Error occurred in PlayerAdded function: " .. error)
    end
end)

I’ve made some changes to the respective lastLoginDate and currentDate check(s). Before making these changes, what does it output as you go to save?

Where is the script with the PlayerAdded?

what does it output as you go to save?

You mean when you claim the rewards?

1 Like

The one that is located in the ServerScriptService (1st one)

Well, you are right, it does print weird stuff

I don’t have screenshots and my friends is busy at the moment to re-create it, but it prints out my name first, everything works, then his, and then his again (and we’re not entirely sure why)

And in some other cases, it doesn’t print his name completely

Could you try the script version I posted and let me know what it outputs? It includes error handling for the wrapped pcall function.

1 Like

What do the print statements show? Are there any error messages or inconsistencies in the data?