Premium and friend multipliers not working at all

My Goal
I want to add these 2 features in my game where the player gains a multiplier for being a Premium player and/or for playing the game with one or more friends.

I want the premium multiplier to be 150% or 1.5x and the friend multiplier to be 200% or 2x. The multipliers do stack on each other if the player both is a premium player and has friends in the game with them but since my script hasn’t been functioning it’s ok if someone figures out a way that works but without them stacking and instead selecting the multiplier (friend multiplier) that is larger.

I don’t need someone to write the entire script for me. I just need help to figure out the bug in my script so these features begin working like I want them to.

The Problem

When I play with my friends, the game doesn’t multiply the base amount of gems that I earn every 5 minutes that I play the game, so I only get 25 gems instead of 50 gems when playing with friends, 37.5 gems when playing with premium, or 62.5 when playing with friends and having Roblox Premium.

The Roblox Premium feature might work since I don’t have Roblox Premium and I can’t test it out myself but I tested out the friends feature and neither me nor my friend earned 50 gems per 5 minutes of playing my game.

What I Tried Doing to Fix This Problem

I tried to look for solutions on the Developer Hub from posts similar to mine but there were none that I could find from searching. I couldn’t find any tutorials on YouTube. I tried asking ChatGPT for help to make this script (I’m still new to scripting) and it doesn’t work.

Here’s my script for checking for premium and friends and providing the multipliers when the gems are updated to the leaderstats gems variable.

local RemoteEvent = game.ReplicatedStorage:WaitForChild("PlayerFriendCheck")

-- Check if a player has Roblox Premium
local function hasPremium(player)
	return player.MembershipType == Enum.MembershipType.Premium
end

-- Table to store player friend statuses
local playerFriendStatuses = {}

-- Update Gems every 5 minutes
local function updateGems()
	while true do
		for _, player in pairs(game.Players:GetPlayers()) do
			if player:FindFirstChild("leaderstats") then
				local Gems = player.leaderstats:FindFirstChild("Gems")
				if Gems then
					local gemMultiplier = 1 -- Base multiplier

					local hasFriends = playerFriendStatuses[player.UserId] or false

					if hasPremium(player) and hasFriends then
						gemMultiplier = 2.5 -- 2.5x more gems
					elseif hasPremium(player) then
						gemMultiplier = 1.5 -- 1.5x more gems
					elseif hasFriends then
						gemMultiplier = 2 -- 2x more gems
					end

					Gems.Value = Gems.Value + math.floor(25 * gemMultiplier)
				end
			end
		end
		wait(300) -- Wait for 5 minutes
	end
end

-- Check for client friend status updates
RemoteEvent.OnServerEvent:Connect(function(player, hasFriends)
	playerFriendStatuses[player.UserId] = hasFriends
end)

-- Ensure player data is cleared when they leave
game.Players.PlayerRemoving:Connect(function(player)
	playerFriendStatuses[player.UserId] = nil
end)

spawn(updateGems)

Here is a screenshot showing that I properly made my my remote events in case people think that’s the problem.

image

Lastly, in case you need to see the leaderstats script to figure out why the multiplier isn’t connecting to the gems update, here it is:

-- Declaring datastore, HTTP, and replicated storage
local dataStoreService = game:GetService("DataStoreService")
local mainDataStore = dataStoreService:GetDataStore("MainStore")
local http = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local defaultData = {
	["Gems"] = 0,
	["Lessons"] = 0,
}

-- Dictionary to store completed lessons per player
local completedLessons = {}

-- Using lessonCompleted to add to lessons variable on leaderboard when player finishes a lesson
local lessonCompletedEvent = ReplicatedStorage:WaitForChild("LessonCompleted")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"

	local Gems = Instance.new("IntValue", folder)
	Gems.Name = "Gems"

	local Lessons = Instance.new("IntValue", folder)
	Lessons.Name = "Lessons"

	local data

	local success, err = pcall(function()
		local plrData = mainDataStore:GetAsync(player.UserId)
		if plrData then
			plrData = http:JSONDecode(plrData)
			data = plrData
		else
			data = defaultData
		end
	end)

	if err then
		data = defaultData
	end

	Gems.Value = data["Gems"]
	Lessons.Value = data["Lessons"]
end)

game.Players.PlayerRemoving:Connect(function(player)
	local plrData = {
		["Gems"] = player.leaderstats.Gems.Value,
		["Lessons"] = player.leaderstats.Lessons.Value,
	}

	local success, err = pcall(function()
		local data = http:JSONEncode(plrData)
		mainDataStore:SetAsync(player.UserId, data)
	end)

	if err then
		warn(err)
	end
end)

-- Function to update Lessons on leaderboard
local function updateLessons(player)
	if player and player:FindFirstChild("leaderstats") then
		local Lessons = player.leaderstats:FindFirstChild("Lessons")
		if Lessons then
			-- Check if the player has already completed this lesson
			if not completedLessons[player.UserId] then
				completedLessons[player.UserId] = true
				Lessons.Value = Lessons.Value + 1
			end
		end
	end
end

-- Checks the LessonCompleted RemoteEvent
lessonCompletedEvent.OnServerEvent:Connect(function(player)
	updateLessons(player)
end)

Please tell me if you need to see any other scripts to help me with this bug, and thank you so much for taking the time out of your day to help me program this feature!

Can we see the code for where this RemoteEvent PlayerFriendCheck is fired?

1 Like

Yes of course!

local player = game.Players.LocalPlayer
local RemoteEvent = game.ReplicatedStorage:WaitForChild("PlayerFriendCheck")

local function checkFriends()
	local friends = player:GetFriendsOnline()
	local friendInGame = false

	for _, friend in pairs(friends) do
		if game.Players:FindFirstChild(friend.Username) then
			friendInGame = true
			break
		end
	end

	RemoteEvent:FireServer(friendInGame)
end

-- Check friends when the player joins
checkFriends()

-- Check friends periodically
while true do
	checkFriends()
	wait(300) -- Check every 5 minutes
end

Thanks for reminding me to add this script so you can see what’s wrong with it.

By the way, if this helps, I’m getting this error message from the output console that’s warning me that the script isn’t working but the language is foreign to me as I’m not very experienced yet.

07:33:55.120 Argument 1 missing or nil -
Client -
GemsMultiplierCheckFriends:9

Capitalization typo here. Should be friend.UserName, not friend.Username. (I know it’s slightly ridiculous, but it’s true lol)

By the way, you can remove this line as you’ve already placed a while true loop below that checks the Player’s friends anyways.

1 Like

Wow that problem came from capitalization? I didn’t expect it to be UserName instead of Username at all.

Thank you for helping me with that!

The only bad news is I tested the game out with two players and one server and it gave me this error message:

HTTP 400 (Bad Request) - Client - GemsMultiplierCheckFriends:5

This error might be from the fact that I can’t send friend requests when testing the game and maybe that’s the problem.

I’ll be back in 10 minutes testing it in-game with my friend to either let you know that it’s still not functioning properly after the fix or to mark your answer as a solution and thank you.

1 Like

Just wanted to let you know that another reason may be that you didn’t wrap the following function in a pcall:

As :GetFriendsOnline() is a function dependent on the network, there is always a chance that it may fail, thus it’s always good practice to wrap network functions in pcalls. This allows your Script to handle errors instead of making the whole script break. You may want to view the Code Sample provided in the documentation of GetFriendsOnline()!

Thank you so much @HelpSupport01

The problem was the capitalization on friend.Username that was supposed to instead be friend.UserName.

I’m not going to lie, I had to re-read that sentence like three times to notice the difference being that Name was capitalized haha.

Thank you so much for your help and I hope you have a wonderful day!

1 Like