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

How to get local player in DataStore2?

Asked by 4 years ago

I was following RobloxDevForum tutorial on making DataStore2, where they used "DATASTORE:increment()" But i found problem, I tried to make money transfer system to give money to any player. It works like this > 1. Type player name 2. Type amount 3. Press transfer But I cant get local player for identify the "Transfer, amount and player name" button. I also cant create "local cashDataStore = DataStore2("Cash",plr)" cause this script uses plr, which i got by PlayerAdded, sadly playeradded isnt looped so i cant use it for making GUI work. Here is the script.

local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))

local defaultValue = 15000

amount = 250

timedelay = 60
currency = "Cash"

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

    local cashDataStore = DataStore2("Cash",plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("IntValue",leaderstats)
    cash.Name = "Cash"

    local function cashUpdate(UpdatedValue)

        cash.Value = cashDataStore:Get(UpdatedValue)
    end
    cashUpdate(defaultValue)
    cashDataStore:OnUpdate(cashUpdate)
end)

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

local button = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").TextButton
local Amount = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").Amount.Text
local Player = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").Player.Text
local CashDataStore = DataStore2("Cash",player)
local function onButtonActivated()
    CashDataStore:Icrement(-Amount)
end

button.Activated:Connect(onButtonActivated)
end)

breakdown at line 29, the problematic place. Another problem is, i dont know how to actually make the transfer, cause it uses increment, and i dont really know how to change others player increment/amount of money with this script, increment works only for local player (by PlayerAdded). Any help from average scripters or with scripters who already had the same problem and know the solve?

0
We'd need to see the module of the datastore to be able to help out some more. Shawnyg 4330 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

Why can't you just use a RemoteEvent? Descendants of PlayerGui aren't visible to the server anyway, so what you're doing is logically flawed. You can pass your target player's name through the remote and simply find the player by name in the Players service, and increment with gained player instance:

-- LocalScript
local player = game.Players.LocalPlayer
local Remote = game.ReplicatedStorage.FunkyRemote

local button = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").TextButton
local Amount = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").Amount.Text
local Player = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").Player.Text

button.MouseButton1Click:Connect(function()
    Remote:FireServer(Player,  Amount)
end)
-- Server script
local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
local defaultValue = 15000

local Remote = Instance.new("RemoteEvent")
Remote.Name = "FunkyRemote"
Remote.Parent = game:GetService("ReplicatedStorage")

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

    local cashDataStore = DataStore2("Cash",plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("IntValue",leaderstats)
    cash.Name = "Cash"

    local function cashUpdate(UpdatedValue)

        cash.Value = cashDataStore:Get(UpdatedValue)
    end
    cashUpdate(defaultValue)
    cashDataStore:OnUpdate(cashUpdate)
end)

Remote.OnServerEvent:Connect(function(player, target, amount)
    target = game.Players:FindFirstChild(target) -- Get player by given name
    if not target then return end -- Invalid player
    local CashDataStore = DataStore2("Cash", target)
    CashDataStore:Increment(-amount)
end)
0
but how will target get the money? digameschannel 44 — 4y
0
CashDataStore:Increment(-amount) will cause the cashUpdate function to be called, which will have the target player's "Cash" instance. From the line cash.Value = cashDataStore:Get(UpdatedValue), the cash will be updated. You must remember that though they aren't in the same scope, the Increment function will still invoke the OnUpdate callback. ankurbohra 681 — 4y
Ad

Answer this question