Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to perform arithmetic (mul) on number and nil?

Asked by 4 years ago

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 -

1script.Parent.MouseButton1Click:connect(function()
2    local amount = script.Parent.Parent.Parent.TextBox.Text
3    amount = tonumber(amount)
4    print(amount)
5    game.ReplicatedStorage.rebirth:FireServer(amount)
6end)

ServerScriptService rebirth script -

01game.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)
14end)

1 answer

Log in to vote
0
Answered by 4 years ago

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

01local player = game.Players.LocalPlayer
02local playerGui = player:WaitForChild("PlayerGui")
03script.Parent.MouseButton1Click: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
13end)

Server Script

1game.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()
6end)

(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.)

Ad

Answer this question