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

Why is my SurfaceGui button that decreases a value when you click it not working?

Asked by 3 years ago
Edited 3 years ago

WATCH THIS IF YOU DONT UNDERSTAND WHAT IM SAYING: https://streamable.com/gwjcgm

I've made a SurfaceGui button with a RemoteEvent that should decrease the value "PrinterLimit" in game.Players.LocalPlayer, but it just does not work and gives me the error: "player argument must be a Player object" or "Argument 1 missing or nil".

Here's the script inside the button that should decrease the value.

local repStorage = game:GetService("ReplicatedStorage")
local DeletePrinter = repStorage.DeletePrinter

script.Parent.MouseButton1Click:Connect(function(Player)
    DeletePrinter:FireClient(Player)
    Player.PrinterLimit.Value = Player.PrinterLimit.Value - 1
end)

And the LocalScript inside StarterPlayerScripts called DecreasePrinterLimit

local repStorage = game:GetService("ReplicatedStorage")
local DeletePrinter = repStorage.DeletePrinter

DeletePrinter.OnClientEvent:Connect(function(player)
    print(player.Name.."just destroyed a printer!")
end)

1 answer

Log in to vote
0
Answered by 3 years ago

Two things: .MouseButton1Click doesn't have any arguments. Second: .OnClientEvent doesn't have player as the first argument, only the arguments you pass by :FireClient(). So fixing those problems you have:

-- server script
local repStorage = game:GetService("ReplicatedStorage")
local DeletePrinter = repStorage.DeletePrinter

game.Players.PlayerAdded:Connect(function(player) -- we are gonna get the player from the playerAdded event

    script.Parent.MouseButton1Click:Connect(function()

        DeletePrinter:FireClient(player)
        player.PrinterLimit.Value -= 1 -- use -= operator

    end)

end)

and

-- local script
local repStorage = game:GetService("ReplicatedStorage")
local DeletePrinter = repStorage:WaitForChild("DeletePrinter") -- just in case
local player = game.Players.LocalPlayer -- we got the player here

DeletePrinter.OnClientEvent:Connect(function() -- no player argument

    print(player.Name.."just destroyed a printer!")

end)

Hope this fixed your problem

0
Works perfectly, thank you! Sorry if this question was a little bit too basic but I'm having a major problem with Server to Client remotevents, now I understand them a bit more, thank you so much! ElevateTheSecond 11 — 3y
0
No problem User#32819 0 — 3y
Ad

Answer this question