I am trying to make a rebirth GUI system where the user inputs the amount of rebirths they want and it will send the inputted amount through a remote event to serverscriptservice where it adds the inputted amount of rebirths. However I get this error -
ServerScriptService.rebirth:4: attempt to perform arithmetic (mul) on number and nil
Layout -
https://i.ibb.co/b2PcvYG/Capture.png
Local Rebirth Script -
1 | script.Parent.MouseButton 1 Click:connect( function () |
2 | local amount = script.Parent.Parent.Parent.TextBox.Text |
3 | amount = tonumber (amount) |
4 | print (amount) |
5 | game.ReplicatedStorage.rebirth:FireServer(amount) |
6 | end ) |
ServerScriptService rebirth script -
01 | game.Players.PlayerAdded:Connect( function (player, amount) |
02 | game.ReplicatedStorage.rebirth.OnServerEvent:Connect( function (player) |
03 | local leaderstats = player:FindFirstChild( "leaderstats" ) |
04 | if leaderstats.Money.Value > = 1000000 * amount then |
05 | leaderstats.Rebirths.Value = leaderstats.Rebirths.Value + amount |
06 | leaderstats.Money.Value = 0 |
07 | player.Character.Head:Destroy() |
08 | else |
09 | player.PlayerGui.Gui.badframe.Visible = true |
10 | wait( 1 ) |
11 | player.PlayerGui.Gui.badframe.Visible = false |
12 | end |
13 | end ) |
14 | end ) |
Firstly, game.Players.PlayerAdded only accepts 1 parameter, which is the new player. Secondly, you can't manipulate the GUI on the server, you have to do it on the client. Thirdly, you can send tuple arguments with the FireServer()
function, however, the first parameter has to be the player. So what I suggest you do is on the client, you change the playerGui that way, and on the server, you change the leaderstats only.
Local Script
01 | local player = game.Players.LocalPlayer |
02 | local playerGui = player:WaitForChild( "PlayerGui" ) |
03 | script.Parent.MouseButton 1 Click:Connect( function () --connect is deprecated. Use Connect. |
04 | local amount = script.Parent.Parent.Parent.TextBox.Text |
05 | amount = tonumber (amount) --This is VERY vulnerable, always change the values on the server. Exploiters can exploit it. |
06 | if player.leaderstats.Money.Value > = 1000000 * amount then --This is only reading the value, which is fine on the client. |
07 | game.ReplicatedStorage.rebirth:FireServer(amount) |
08 | else |
09 | player.PlayerGui.Gui.badframe.Visible = true |
10 | wait( 1 ) |
11 | player.PlayerGui.Gui.badframe.Visible = false |
12 | end |
13 | end ) |
Server Script
1 | game.ReplicatedStorage.rebirth.OnServerEvent:Connect( function (player, amount) |
2 | local leaderstats = player:WaitForChild( "leaderstats" ) --This is a better alternative since you need it. |
3 | leaderstats.Rebirths.Value = leaderstats.Rebirths.Value + amount |
4 | leaderstats.Money.Value = 0 |
5 | player.Character.Head:Destroy() |
6 | end ) |
(Note on the amount variable. Always change the values on the server. You can use an IntValue and place it ReplicatedStorage. But that's kind of off-topic.)