I want that when a player types another player's name that is in the server and types the value of what they want to give, then submits it, it will add the amount of what the player typed to the other player's leaderstats value.
01 | local PlrName = script.Parent.Parent.PlrName |
02 | local moneytogive = tonumber (script.Parent.Parent.Value.Text) -- "Value" is a TextBox |
03 | local ting = 0 -- debounce |
04 |
05 | function check() |
06 | if ting = = 0 then |
07 | ting = 1 |
08 | local target = game.Players:FindFirstChild(PlrName) |
09 | if target ~ = nil then |
10 | local stats = target.leaderstats |
11 | if stats ~ = nil then |
12 | local points = stats.Points |
13 | points.Value = points.Value + moneytogive |
14 | wait() |
15 | end |
That is what I have done, someone tell me why it won't work and how I can fix it.
01 | local amountToGive = script.Parent.amount --TextBox where the player puts the amount of cash to give |
02 | local playerName = script.Parent.player --TextBox where the player types another players' name |
03 | local submit = script.Parent.submit --TextButton used to submit form to donate cash to player |
04 | submit.MouseButton 1 Click:Connect( function () --When clicked |
05 | local playerToGive = game:GetService( "Players" ):FindFirstChild(playerName.Text) --Player who receives donation |
06 | local playerWhoClicked = game.Players.LocalPlayer --Player who wants to donate |
07 | if playerToGive then --If the player we typed in is actually there |
08 | if playerWhoClicked.leaderstats.Cash.Value > = amountToGive.Text then --If the player who wants to donate has enough cash |
09 | playerToGive.leaderstats.Cash.Value = playerToGive.leaderstats.Cash.Value + amountToGive.Text --Give cash to player |
10 | playerWhoClicked.leaderstats.Cash.Value = playerWhoClicked.leaderstats.Cash.Value - amountToGive.Text --Subtracts the cash from the player who donated |
11 | end |
12 | end |
13 | end ) |
Just make sure this is in a LocalScript so LocalPlayer can be indexed! Hierarchy here. Please accept my answer if this helped!
Since the Text in the TextBox is a string, you cannot use 'tonumber', or need a 'tostring'. Here's the fixed version
01 | local PlrName = script.Parent.Parent.PlrName |
02 | local moneytogive = script.Parent.Parent.Value.Text |
03 | local ting = 0 -- debounce |
04 |
05 | function check() |
06 | if ting = = 0 then |
07 | ting = 1 |
08 | local target = game.Players:FindFirstChild(PlrName) |
09 | if target ~ = nil then |
10 | local stats = target.leaderstats |
11 | if stats ~ = nil then |
12 | local points = stats.Points |
13 | points.Value = points.Value + moneytogive |
14 | wait() |
15 | end |