Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a leaderstats value change by a ScreenGui input?

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.

local PlrName = script.Parent.Parent.PlrName
local moneytogive = tonumber(script.Parent.Parent.Value.Text) -- "Value" is a TextBox
local ting = 0 -- debounce

 function check()
		if ting == 0 then
			ting = 1
			local target = game.Players:FindFirstChild(PlrName)
				if target ~= nil then
				local stats = target.leaderstats
					if stats ~= nil then
					local points = stats.Points
					points.Value = points.Value + moneytogive
					wait()
				end
			end
			ting = 0
		end
		script.Parent.Parent.PlrName.Text = "Insert Name Here"
		script.Parent.Parent.Value.Text = "Insert Value Here"
	end

script.Parent.MouseButton1Click:connect(check)

That is what I have done, someone tell me why it won't work and how I can fix it.

1 comment

0

If Value is the name of a textbox, you get its contents with Value.Text

2 answers

✓ Accepted answer
local amountToGive = script.Parent.amount --TextBox where the player puts the amount of cash to give
local playerName = script.Parent.player --TextBox where the player types another players' name
local submit = script.Parent.submit --TextButton used to submit form to donate cash to player
submit.MouseButton1Click:Connect(function() --When clicked
	local playerToGive = game:GetService("Players"):FindFirstChild(playerName.Text) --Player who receives donation
	local playerWhoClicked = game.Players.LocalPlayer --Player who wants to donate
	if playerToGive then --If the player we typed in is actually there
		if playerWhoClicked.leaderstats.Cash.Value >= amountToGive.Text then --If the player who wants to donate has enough cash 
		playerToGive.leaderstats.Cash.Value = playerToGive.leaderstats.Cash.Value + amountToGive.Text --Give cash to player
		playerWhoClicked.leaderstats.Cash.Value = playerWhoClicked.leaderstats.Cash.Value - amountToGive.Text --Subtracts the cash from the player who donated
		end
	end
end)

Just make sure this is in a LocalScript so LocalPlayer can be indexed! Hierarchy here. Please accept my answer if this helped!

2 comments

0

sorry, not working it said ,attempted to compare number with string

0

ok nevermind, i fixed that but now it just wont add to the stat's value

Since the Text in the TextBox is a string, you cannot use 'tonumber', or need a 'tostring'. Here's the fixed version

local PlrName = script.Parent.Parent.PlrName
local moneytogive = script.Parent.Parent.Value.Text
local ting = 0 -- debounce

 function check()
        if ting == 0 then
            ting = 1
            local target = game.Players:FindFirstChild(PlrName)
                if target ~= nil then
                local stats = target.leaderstats
                    if stats ~= nil then
                    local points = stats.Points
                    points.Value = points.Value + moneytogive
                    wait()
                end
            end
            ting = 0
        end
        script.Parent.Parent.PlrName.Text = "Insert Name Here"
        script.Parent.Parent.Value.Text = "Insert Value Here"
    end

script.Parent.MouseButton1Click:connect(check)

This discussion is preserved as it was. New answers, comments, and votes are closed.