Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I handle a leader board stat "money" properly?

Asked by 7 years ago
Edited 7 years ago

I will try to explain this as best as I can. I recently followed the tutorial for leaderboard stats since it corresponds a lot with certain things you would want to do in your map. My leader board stat is "money", while I would love to prevent cheating and stuff, to my assumption it makes handling these values in the leader boards more tricky. I myself are very new to lua scripting.

I made a tool that when picked up gives the player handling the tool the ability to spawn an object called a "VIPPART" where ever they click. I got that to work from having the function clone the part from replicated storage. My issue is I want the part to give money only to the person handling the tool. Another issue is when spawning a decent quantity of blocks, the money stat value resets unless you spawn one block and wait awhile. I thought that it could maybe be the game having an overload in data or something like that, and increased the quantity of the money the each block gives by "10" and adding a wait timer of "5". When I spawned a block, my money stat went up by "10" but then after about 4 seconds, it reset.

Me being a detective, singled the problem to the leader board script the tutorial had me make. It appears that with the loop, that adds one "money" every " five seconds, the values reset.

script for leader boards.

--A table for holding all the player's leaderstat values
local playerLeaderstats = {} 

game.Players.PlayerAdded:connect(function(player)
    playerLeaderstats[player] = {}
    playerLeaderstats[player]["Money"] = 0
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = playerLeaderstats[player]["Money"]
    money.Parent = leaderstats
end)

while wait(5) do 
    for _, player in pairs(game.Players:GetPlayers()) do
        --We don't use the IntValue's value because an exploiter can modify it.
        playerLeaderstats[player]["Money"] = playerLeaderstats[player]["Money"] + 1
        if player:FindFirstChild("leaderstats") then
           player.leaderstats.Money.Value = playerLeaderstats[player]["Money"]
        end
    end
end

At the time I did a Copy/Paste of the script since I wasn't adept enough to fully understand it. Now I understand most of it, if not all of it. I'm just not sure what I should do or add to make this work.

VIPWAND script:

local vipwandcooldown = false
    function addMoney()
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Money.Value = Player.leaderstats.Money.Value +1
        end
    end
    end

        function addTenMoney()
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Money.Value = Player.leaderstats.Money.Value +10
        end
    end
end


-- Server Script
local tool = script.Parent
local clickEvent = tool.ClickEvent
local clickEventConnection


local function createPart(location)
    if vipwandcooldown == false then
    local part = game.ReplicatedStorage.VIPPART:Clone()
    addTenMoney()
    part.Name = "VIPPART"
    part.Parent = game.Workspace
    part.CFrame = location
    part.BrickColor = BrickColor.Random()
    vipwandcooldown = true
        if part.BrickColor == BrickColor.Yellow()then
        part.Material = Enum.Material.Neon
        print("Luck Block! +50 money")
        end
    wait (5)
    part.Material = Enum.Material.Metal
    part:Destroy()
    vipwandcooldown = false
    end
end

local function onClick(player, clickLocation)
    createPart(clickLocation)
end

local function onEquip()
    clickEventConnection = clickEvent.OnServerEvent:connect(onClick)
end

local function onUnequip()
    clickEventConnection:disconnect()
end

tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)

The "lucky block" is not yet configured.

To my understanding, these are the only scripts needed to be seen.

Sorry that I'm a noob at this, but I have a passion to learn.

In a sense I know what needs to be done, I just don't know how to do it properly.

Thank you all for helping in advance, I'm still learning so most of this is copy/paste I however know what all of means, I just wouldn't know how to write it without a reference yet.

Answer this question