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

How do I get this Robbery system to take the money that's "stolen" and store it in my DataStorage?

Asked by 4 years ago

I've tried multiple methods but I just can't seem to get it to work, I'd really appreciate some support.

I absolutely do not want to use the standard ROBLOX leaderboard, but the moneySystem I've put together that makes use of ScreenGuis, sorry if this is a really simple fix but I'm quite new to scripting!

bankCashGiver

local debounce = false
local playerfound = false
local player = game:GetService("Players")

--local moneyData = Instance.new("Folder",game.ReplicatedStorage)
--moneyData.Name = "MoneyData" -- Once the game starts up, there will be a folder created in ReplicatedStorage.

local cash = game:GetService("DataStoreService"):GetDataStore("MoneyData")

--local dataStoreService = game:GetService("DataStoreService"):GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:Connect(function(plr)
    --local plrCash = game:GetService("DataStoreService"):GetDataStore("MoneyData")
    --plrCash.Name = plr.Name -- Once the game starts up, a personal data folder will be created inside of "MoneyData." 

    local key = "id-" .. plr.UserId -- Requires a passcode in order to change, access, or modify data.

        pcall(function()
        saveData = cash:GetAsync(key)
    end)
    if saveData then
        cash.Value = saveData[1]
    else
        local save = {cash} -- This'll create a data table if the Player hasn't played before.
        pcall(function()
            cash:SetAsync(key,save)
        end)
    end
end)

function CreateRegion3FromPart(Part)
    return Region3.new(Part.Position-(Part.Size/2),Part.Position+(Part.Size/2))
end

function GetPlayersInPart(part)
    local region = CreateRegion3FromPart(part)
    local partsInRegion = workspace:FindPartsInRegion3(region,nil,math.huge)
    local Players = {}


    for i,Part in pairs(partsInRegion) do
        local player = game.Players:GetPlayerFromCharacter(Part.Parent)
        if player then

            for i, v in pairs(Players) do
                if Players[i].Name == player.Name then
                    playerfound = true
                end
            end

            if playerfound == false then
                table.insert(Players,player)
            end
            playerfound = false
        end
    end

    for i, v in pairs(Players) do

        if Players[i].Robbing.Value == false then
            game.ReplicatedStorage.ShowRobberyGUI:FireClient(Players[i])
        end
        Players[i].Robbing.Value = true
        if Players[i].cashCollectedData.Value < 1500 then -- This is how much money the player can rob at a time.
            Players[i].cashCollectedData.Value = Players[i].cashCollectedData.Value + 10 -- This is how much money the player will receive every {number} second(s).
        end
    end
    return Players
end

while wait(1) do -- This changes how long it takes the player before the receive "Players[i].cashCollectedData.Value + {number}"
    GetPlayersInPart(game.Workspace.Detector)
end

DoorDetector (whenever you leave the building it would deposit the money into the account.)

local replicatedStorage = game.ReplicatedStorage
local MoneyData = game.ReplicatedStorage:FindFirstChild("MoneyData")

game.Players.PlayerAdded:Connect(function(plr)
    local MoneyData = game.ReplicatedStorage:FindFirstChild("MoneyData")
    local sourcePlayer = MoneyData:FindFirstChild(plr.Name)

end)


script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("HumanoidRootPart") then
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player.Robbing.Value == true then
            game.ReplicatedStorage.HideRobberyGUI:FireClient(player)
            replicatedStorage.MoneyData.sourcePlayer.Cash.Value = replicatedStorage.MoneyData.sourcePlayer.Cash.Value + replicatedStorage.cashCollectedData.Value
            replicatedStorage.cashCollectedData.Value = 0
            replicatedStorage.Robbing.Value = false
        end
    end
end)

"Leaderboard"

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = game.ReplicatedStorage:GetChildren("MoneyData")
    leaderstats.Name = "MoneyData"

    local money = game.ReplicatedStorage.MoneyData:GetChildren("Cash")
    money.Name = "Cash"

    local cc = Instance.new("IntValue", player)
    cc.Name = "cashCollectedData"

    local robbing = Instance.new("BoolValue", player)
    robbing.Name = "Robbing"
    robbing.Value = false

    player.CharacterAdded:connect(function(char)
        player.Robbing.Value = false
        player.cashCollectedData.Value = 0
    end)

end)

Actual money GUI "LocalScript"

local player = game.Players.LocalPlayer

wait() -- Allows the Client to load in before the Server interacts, avoiding any corruption/crashing.
local moneyData = game.ReplicatedStorage:WaitForChild("MoneyData")
local playerCash = moneyData:WaitForChild(player.Name)

script.Parent.cashDisplay.Text = "$ "..playerCash.Cash.Value
playerCash.Cash.Changed:connect(function()
    script.Parent.cashDisplay.Text = "$ "..playerCash.Cash.Value -- Updates the GUI whenever the Player earns a new amount of cash.
end)

Any support would be heavily appreciated, been stumped for quite some time now! I should be able to provide any information if you need anything else.

0
Just to clarify, you have to money stolen and when the player leaves the game, you what it to save? XviperIink 428 — 4y
0
Yes, once you leave the building (transparent part in the doorway) you'll receive the money and it'll save whenever the Player leaves then rejoins. ofek163 7 — 4y
0
Where's the attempts and how did it not work? Also you shouldn't rely on random arbritary waits since they aren't reliable hiimgoodpack 2009 — 4y

Answer this question