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

why does this glitch when i enter the value '1'?

Asked by
gunter5 17
6 years ago

Everything works exceptionally fine, But i do get a couple issues. What the below code does is handle player data when exchanging one value to another (cash to bank and vise vera) the issue is, when i enter a value of '1' into the text box for a deposit and i have 0$ cash on me. i will draw out sum of cash equal to what was deposited before i deposited the '1' dollar (im trying to deposit 1 dollar to see if any bugs occur and sure enough)

NPC in the workspace.

local data = require(game:GetService("ServerScriptService"):WaitForChild("CoreScripts"):WaitForChild("Modules"):WaitForChild("DataHandler"))
local RE = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("InteractionRemote")
local RE2 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("LeaveRemote")
local RE3 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("TypeRemote")
local RE4 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("TransactionRemote")
local RE5 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("BalanceRemote")
local npc = script.Parent
local cd = npc.Parent:WaitForChild("Desk"):WaitForChild("Mic"):WaitForChild("ClickDetector")
local gui = npc.Head.BillboardGui
local NPCName = "Teller"
local message = "What can i do for ya?"
local message2 = "So you wanna make a transaction, or check your balance?"
local message3 = "You don't have enough to do that."
local chattime = .05
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=1258082161"
local idle = npc.Humanoid:LoadAnimation(animation)
local animation2 = Instance.new("Animation")
animation2.AnimationId = "http://www.roblox.com/Asset?ID=1258030744"
local lookaround = npc.Humanoid:LoadAnimation(animation2)

idle:play()
spawn(function()
    while wait(15) do
        lookaround:play()
    end
end)

RE.OnServerEvent:connect(function(player)
    local char = workspace:WaitForChild(player.Name)
    char:WaitForChild("Humanoid").WalkSpeed = 0
    char:WaitForChild("Humanoid").JumpPower = 0
    RE:FireClient(player, NPCName, message, chattime)
end)

RE2.OnServerEvent:connect(function(player) -- Leave selected
    local char = workspace:WaitForChild(player.Name)
    char:WaitForChild("Humanoid").WalkSpeed = 16
    char:WaitForChild("Humanoid").JumpPower = 50
end)

RE3.OnServerEvent:connect(function(player) -- Transaction selected
    RE3:FireClient(player, NPCName, message2, chattime, data.CurrentData(player).Cash, data.CurrentData(player).Bank)
end)

RE4.OnServerEvent:connect(function(player, transtype, amount, cash, bank) -- transaction made, update datastore.
    data.UpdateData(player, "Cash", cash)
    data.UpdateData(player, "Bank", bank)
    if transtype then
        local message5 = "Deposited $"..amount.." successfully."
        RE4:FireClient(player, NPCName, message5, chattime)
    elseif not transtype then
        local message5 = "Withdrew $"..amount.." successfully."
        RE4:FireClient(player, NPCName, message5, chattime)
    end
end)

RE5.OnServerEvent:connect(function(player) -- Check Balance
    local balance = "You have a total of $"..data.CurrentData(player).Bank.." in your bank account."
    RE5:FireClient(player, NPCName, balance, chattime)
end)

local script in a gui

local player = game:GetService("Players").LocalPlayer
local RE = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("InteractionRemote")
local RE2 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("LeaveRemote")
local RE3 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("TypeRemote")
local RE4 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("TransactionRemote")
local RE5 = game:GetService("ReplicatedStorage"):WaitForChild("BankRemotes"):WaitForChild("Bank00"):WaitForChild("BalanceRemote")
local dialog = script.Parent
local choices = dialog.Choices
local choices2 = dialog.Choices2
local Transaction = choices.Transaction.Button
local leave = choices.Leave.Button
local withdraw = choices2.Withdraw.Button
local deposit = choices2.Deposit.Button
local balance = choices2.Balance.Button
local chat = dialog.dialogbox
local sound = dialog.Sound
local transframe = dialog.TransactionFrame
local transinfo = transframe.Type
local amount = transframe.Amount
local cd = workspace.MapContents.BuildingContents.Bank00.Interior:WaitForChild("Desk"):WaitForChild("Mic"):WaitForChild("ClickDetector")
local mark = workspace.MapContents.BuildingContents.Bank00.Interior.NPC.Head.BillboardGui
local active = false

cd.MouseHoverEnter:connect(function(player)
    mark.Enabled = true
end)

cd.MouseHoverLeave:connect(function(player)
    mark.Enabled = false
end)

cd.MouseClick:connect(function()
    dialog.Enabled = true
    RE:FireServer()
end)

Transaction.MouseButton1Click:connect(function()
    choices.Visible = false
    RE3:FireServer()
end)

leave.MouseButton1Click:connect(function()
    dialog.Enabled = false
    choices.Visible = false
    chat.Visible = false
    RE2:FireServer()
end)

local function deposittransaction(cash, bank) -- Check transaction type then make transaction
    transframe.Visible = true
    chat.Visible = false
    choices2.Visible = false
    amount:CaptureFocus()
    local transtype = true
    transinfo.Text = '- Deposit -'
    amount.FocusLost:connect(function()
        if not tonumber(amount.Text) then
            transinfo.Text = 'You must enter a number!'
            wait(1)
            transinfo.Text = '- Deposit -'
        elseif cash >= tonumber(amount.Text) then
            cash = cash - tonumber(amount.Text)
            bank = bank + tonumber(amount.Text)
            RE4:FireServer(transtype, amount.Text, cash, bank)
            RE2:FireServer()
        elseif cash < tonumber(amount.Text) then
            transinfo.Text = 'You dont have enough to do that!'
            wait(1)
            transinfo.Text = '- Deposit -'
        end
    end)
end

local function withdrawtransaction(cash, bank) -- Check transaction type then make transaction
    transframe.Visible = true
    chat.Visible = false
    choices2.Visible = false
    amount:CaptureFocus()
    local transtype = false
    transinfo.Text = '- Withdrawal -'
    amount.FocusLost:connect(function()
        if tonumber(amount.Text) == nil then
            transinfo.Text = 'You must enter a number!'
            wait(1)
            transinfo.Text = '- Withdrawal -'
        elseif bank >= tonumber(amount.Text) then
            cash = cash + tonumber(amount.Text)
            bank = bank - tonumber(amount.Text)
            RE4:FireServer(transtype, amount.Text, cash, bank)
            RE2:FireServer()
        elseif bank < tonumber(amount.Text) then
            transinfo.Text = 'You dont have enough to do that!'
            wait(1)
            transinfo.Text = '- Withdrawal -'
        end
    end)
end

balance.MouseButton1Click:connect(function() -- Check Balance
    RE5:FireServer()
end)

RE.OnClientEvent:connect(function(NPCName, message, chattime) -- Starting Interaction
    chat.Visible = true
    for i = 1, #message do
        chat.TextLabel.Text = NPCName..": "..string.sub(message,1,i)
        sound:Play()
        wait(chattime)
    end
    wait(1)
    choices.Visible = true
end)

RE3.OnClientEvent:connect(function(NPCName, message, chattime, cash, bank) -- Transaction Selected
    for i = 1, #message do
        chat.TextLabel.Text = NPCName..": "..string.sub(message,1,i)
        sound:Play()
        wait(chattime)
    end
    wait(1)
    choices2.Visible = true
    withdraw.MouseButton1Click:connect(function() -- Withdraw Cash
        withdrawtransaction(cash, bank)
    end)

    deposit.MouseButton1Click:connect(function() -- Deposit Cash
        deposittransaction(cash, bank)
    end)
end)

RE4.OnClientEvent:connect(function(NPCName, message, chattime) -- Make Transaction
    chat.Visible = true
    choices2.Visible = false
    for i = 1, #message do
        chat.TextLabel.Text = NPCName..": "..string.sub(message,1,i)
        sound:Play()
        wait(chattime)
    end
    wait(1)
    transframe.Visible = false
    chat.Visible = false
    dialog.Enabled = false
    RE2:FireServer()
end)

RE5.OnClientEvent:connect(function(NPCName, message, chattime) -- Check Balance
    chat.Visible = true
    choices2.Visible = false
    for i = 1, #message do
        chat.TextLabel.Text = NPCName..": "..string.sub(message,1,i)
        sound:Play()
        wait(chattime)
    end
    wait(1)
    chat.Visible = false
    dialog.Enabled = false
    RE2:FireServer()
end)

module for datastores

local data = {}
local ds = game:GetService("DataStoreService"):GetDataStore("TestingSim2Data_v0.1.0")
local RE = game:GetService("ReplicatedStorage"):WaitForChild("ClientRemotes"):WaitForChild("MoneyRemote")
local players = game:GetService('Players')

sessiondata = {}
local default = {
    Cash = 5000,
    Bank = 9999999999
}

data.UpdateData = function(player, index, value)
    sessiondata[player][index] = value
    RE:FireClient(player, sessiondata[player])
    print("Value "..sessiondata[player][index].." was updated to "..value)
end

local function SaveData(player)
    if sessiondata[player] then
        ds:SetAsync(player.UserId, sessiondata[player])
    end
end

local function SetupData(player)
    local playerdata = data.GetData(player)
    if not playerdata then
        print("setting up player data")
        sessiondata[player] = default
        SaveData(player)
    else
        print("player has data")
        sessiondata[player] = playerdata
    end
end

players.PlayerAdded:connect(function(player)
    SetupData(player)
end)

players.PlayerRemoving:connect(function(player)
    SaveData(player)
    sessiondata[player] = nil
end)

data.GetData = function(player)
    return ds:GetAsync(player.UserId)
end

data.CurrentData = function(player)
    return sessiondata[player]
end

return data

Answer this question