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 5 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.

01local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
02 
03local defaultValue = 15000
04 
05amount = 250
06 
07timedelay = 60
08currency = "Cash"
09 
10game.Players.PlayerAdded:Connect(function(plr)
11 
12    local cashDataStore = DataStore2("Cash",plr)
13    local leaderstats = Instance.new("Folder",plr)
14    leaderstats.Name = "leaderstats"
15 
View all 38 lines...

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 — 5y

1 answer

Log in to vote
1
Answered by 5 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:

01-- LocalScript
02local player = game.Players.LocalPlayer
03local Remote = game.ReplicatedStorage.FunkyRemote
04 
05local button = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").TextButton
06local Amount = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").Amount.Text
07local Player = player.PlayerGui.Phone:WaitForChild("Base"):WaitForChild("Screen"):WaitForChild("AppScreens"):WaitForChild("FunkyPay").Player.Text
08 
09button.MouseButton1Click:Connect(function()
10    Remote:FireServer(Player,  Amount)
11end)
01-- Server script
02local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
03local defaultValue = 15000
04 
05local Remote = Instance.new("RemoteEvent")
06Remote.Name = "FunkyRemote"
07Remote.Parent = game:GetService("ReplicatedStorage")
08 
09game.Players.PlayerAdded:Connect(function(plr)
10 
11    local cashDataStore = DataStore2("Cash",plr)
12    local leaderstats = Instance.new("Folder",plr)
13    leaderstats.Name = "leaderstats"
14 
15    local cash = Instance.new("IntValue",leaderstats)
View all 31 lines...
0
but how will target get the money? digameschannel 44 — 5y
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 — 5y
Ad

Answer this question