Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.
This script is supposed to pay money to the leaderboard value when a player is on a certain team, but it's not working.


Code:
local Teams = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local jobConfig = {
    ["McWilliam's Worker"] = 650,
    ["Another Job"] = 500,
    ["Yet Another Job"] = 750,
}

local paymentInterval = 300 -- 5 minutes
local paymentNotificationEvent = ReplicatedStorage:WaitForChild("PaymentNotification")

-- Function to pay players in the jobs
local function payPlayers()
    for teamName, paymentAmount in pairs(jobConfig) do  -- Loop through each job
        local team = Teams:FindFirstChild(teamName)
        if team then  -- If the team exists
            for _, player in pairs(team:GetPlayers()) do
                local leaderstats = player:FindFirstChild("leaderstats")
                local money = leaderstats and leaderstats:FindFirstChild("Money")

                if money then
                    money.Value = money.Value + paymentAmount  -- Add money to the player
                    print("Paid " .. player.Name .. " $" .. paymentAmount)
                    paymentNotificationEvent:FireClient(player, paymentAmount)  -- Send notification to player
                else
                    print("Money value not found for player: " .. player.Name)
                end
            end
        else
            print("Team " .. teamName .. " not found")
        end
    end
end

-- Repeat the payPlayers function every 5 minutes
while true do
    wait(paymentInterval)
    payPlayers()
end
 
Hi thegamer_guy,

It looks like the script is almost there, but there might be a few potential reasons it's not working as expected.

  1. Check for leaderstats: Make sure that leaderstats exists for the players. This might be where the issue lies if the script isn't finding the "Money" value. You can add a quick check before the loop to confirm:

    Code:
    if not leaderstats thenprint(player.Name .. " does not have leaderstats")end

  2. Timing Issue: If the script isn't running as expected, the while loop with wait(paymentInterval) might be blocked by something else or the timing is off. Consider using spawn() to run the function in a new thread, or using a delay function.
Let me know if that helps!
 
Hi thegamer_guy,

It looks like the script is almost there, but there might be a few potential reasons it's not working as expected.

  1. Check for leaderstats: Make sure that leaderstats exists for the players. This might be where the issue lies if the script isn't finding the "Money" value. You can add a quick check before the loop to confirm:

    Code:
    if not leaderstats thenprint(player.Name .. " does not have leaderstats")end

  2. Timing Issue: If the script isn't running as expected, the while loop with wait(paymentInterval) might be blocked by something else or the timing is off. Consider using spawn() to run the function in a new thread, or using a delay function.
Let me know if that helps!
I tried that it still won't work, I have leaderstats setup, so i tried the timing issue, but it didn't work either.
 
Hmm, that’s frustrating! Since you’ve already checked the leaderstats and timing, maybe the issue is with how the payment event is firing. Have you checked if the paymentNotificationEvent in ReplicatedStorage is set up properly? You can try printing right before firing the event to see if it’s actually reaching that part of the script.

Also, just to double-check, are there any errors popping up in the output window? Sometimes small errors can sneak by and break the loop.

Let me know what you find!
 
Back
Top Bottom