I don’t fully know how to script it so that when you enter a players name it gives an amount of coins to the selected player if they’re in a server or not.
I was wondering if anyone can show me a tutorial that could help with this or show a script for this. Any help would be appreciated. Thanks.
Ok so how I would do it is I would in a local script do a function and inside that function I would put a variable which then collects up the text from the player name text box. I would then fire an event to the server which contains the player name as a parameter. The player name parameter could either be got from the variable you create or you could instead of having a variable you could just get the text from the text box and put that inside where the parameters will be. On the server side you can then change the amount of coins they have and do all ur stuff like a data store and change the leaderstats or what ever how ur system works.
For the money amount it would be the exact same but you would just add another parameter in the event which is fired to the server with the amount of coins instead of the player. U then just collect both on the server.
The of course u gotta run the function whenever the give button is clicked
You want to put a LocalScript inside the give button, a RemoteEvent in ReplicatedStorage and a Script in ServerScriptService.
In the localScript you want to get the player’s name with local playerName = playerNameBox.Text and the amount with local amount = amountBox.Text then you want to tell to the server to give the money by firing the RemoteEvent game.ReplicatedStorage.RemoteEvent:FireServer(playerName, amount) (use giveButton.MouseButton1Click to know when the button is clicked) and then on the Script in ServerScriptService you want to listed to the RemoteEvent with game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, playeToGiveCoinsName, amount) the player arguments is the player that clicks the button and then you want to add the money to the player and amount is the amount you want to give, if you are using a leaderstats you want to do game.Players:FindFirstChild(playerToGiveCoinsName").leaderstats.Cash.Value = game.Players:FindFirstChild(playerToGiveCoinsName").leaderstats.Cash.Value + tonumber(amount)
You can fire a remote to the server, then the server will try to find the player and give the coins to the player.
Edit: Changed the code a bit so it works.
button.MouseButton1Click:Connect(function()
local number = string.match(amount.Text, "%d+")
if #player.Text > 0 and number then
remote:FireServer(player.Text, tonumber(number))
end
end)
remote.OnServerEvent:Connect(function(player, name, amount)
local toGive = game.Players:FindFirstChild(name)
if player.leaderstats.Coins.Value < amount or not toGive then
return
end
toGive.leaderstats.Coins.Value += amount
player.leaderstats.Coins.Value -= amount
end)
I’ve done this but it’s not seeming to work so I thought I’d give you the process I done and if you could check where I went wrong that would be appreciated.
So first I added a Local Script into the Give Button, then I add a Remote Event which I named “GiveCash” into Replicated Storage after this, I added a Script into Server Script Service and named this GiveCoinsScript.
Here’s what the Local Script includes:
local playerName = script.Parent.Parent.PlayerNameBox.Text
local amount = script.Parent.Parent.AmountBox.Text
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.GiveCoins:FireServer(playerName, amount)
end)
This is because you save the text value when the script loads when the player joins so it saves nil since when you join you don’t fill that TextBox, so what you want to do is move the variables inside of the clicked event or make the variables without the .Text parameter and then add it when firing the RemoteEvent.
Basically when the GUI gets loaded the boxes are nil, there’s no text and the script saves that (nil), in fact it’s not suggested to save the value to a variable if it’s a value that changes like your case.
Example:
Your boxes’ text is nil and the script sets the value to nil because it saves it only the first time the player joins the game so when it is nil, you want to save that value each time you click on the button because you should press the button only when you add a player and an amount in the boxes, also i would suggest to add some checks in one of the scripts to avoid any error that could occur like an invalid amount or player, i suggest to do this on the Server Script by adding:
if game.Players:FindFirstChild(playerToGiveCoinsName) and tonumber(amount) then
Basically what tonumber() does is turn numbers written as text in “real” numbers and if you put letters instead of numbers it will return nil because obviously letters cannot be turned into numbers.
So with the if statement above if the guy that clicks the button doesn’t insert a valid player or a valid amount (letters and not numbers) you will not get errors because the script will just not continue if the controls aren’t satified.
P.S. Sorry i forgot to say that they have to go in the clicked event with that method…
Hope this helped, also remember to check the output to see if you get errors and providing them when asking help would be usefull for the people that try to solve the problem!
So, I’ve been trying something that someone has said above to see if that would help it.
Also, I’m making it so that when I put a Players Username and an Amount it would give the chosen player that amount of Coins.
So, it’s like adding Cash to their account using a GUI.
Also, it’s fine if you couldn’t find anything online, any help will do! Thanks.
I added that line of code to the Server Script and here is what it looks like now:
game.ReplicatedStorage.GiveCoins.OnServerEvent:Connect(function(player, playerToGiveCoinsName, amount)
if game.Players:FindFirstChild(playerToGiveCoinsName) and tonumber(amount) then
game.Players:FindFirstChild(playerToGiveCoinsName).leaderstats.Coins.Value = game.Players:FindFirstChild(playerToGiveCoinsName).leaderstats.Coins.Value + tonumber(amount)
end
end)
I’ve also removed the TextBoxes text for both boxes and instead put the text in the placeholder text. After I made some changes, I tried it again on myself (not too sure if that’s a problem) and it didn’t give the amount but did not show any errors. I’ll have a look more into this and see if anything is wrong.
I don’t understand the change you did to the PlaceHolderText…Can i have the LocalScript code too? Also try to add a print function after the check in the ServerScript print(playerToGiveCoinsName, tonumber(amount))
The problem wasn’t that the box description that you have changed to PlaceholderText but the fact that you save the text when the script is loaded so it will be nil because when you join the game that box is empty and the script saves that empty value so when you click it you send to the server nil and it doesn’t work
There are two ways you can follow:
1
local playerName = script.Parent.Parent.PlayerNameBox
local amount = script.Parent.Parent.AmountBox
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.GiveCoins:FireServer(playerName.Text, amount.Text)
end)
2
script.Parent.MouseButton1Click:Connect(function()
local playerName = script.Parent.Parent.PlayerNameBox.Text
local amount = script.Parent.Parent.AmountBox.Text
game.ReplicatedStorage.GiveCoins:FireServer(playerName, amount)
end)
So in the 1st one you create a variable of the boxes and save the text when the button is clicked, in the 2nd you create a variable of the text when the button is clicked, the way you did it won’t work as the script saves nil because when you join the game it instantly saves nil as the box is nil if you don’t insert a text, hope you can understand what i mean.