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

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

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

✓ Accepted answer

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

2 comments

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!

0

No problem

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