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.
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!