Help with remote event

So I am trying to make an auto clicker game pass in my game but I have run into a problem with the system I am using. That is I am using a remote event to contact the server and update the leaderstats

my local script for the button -

local amount = 1
local timevalue = 0.5
local boolValue = script.Parent.StopButtonPressed
local player = game.Players.LocalPlayer
local rem = game.ReplicatedStorage.AutoclcikerAdder
local gamepassId = 22468201
local mp = game:GetService("MarketplaceService")


script.Parent.Parent.AutoclcikerOFF.MouseButton1Click:Connect(function()
	boolValue.Value = true
	script.Parent.Visible = true
	script.Parent.Parent.AutoclcikerOFF.Visible = false
end)
if mp:UserOwnsGamePassAsync(player.UserId,gamepassId) then
	script.Parent.MouseButton1Click:Connect(function()
		
		script.Parent.Visible = false
		script.Parent.Parent.AutoclcikerOFF.Visible = true
		boolValue.Value = false
		repeat wait(timevalue)
			rem:FireServer("true")

		until boolValue == true 
		

	end)
else
	mp:PromptGamePassPurchase(player,gamepassId)
end

My server code receiving the onserverevent -

local rem = game.ReplicatedStorage.AutoclcikerAdder

if game.StarterGui.Guis.AutoclickerOn.StopButtonPressed.Value == false then
	rem.OnServerEvent:Connect(function(player)
		player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1
	end)
else
	print("Stopped")
end

So when the other button is pressed it isnt stopping it

the layout of the gui -
image

in this block of code you list it as game.StarterGui.Guis.AutoclickerOn.StopButtonPressed
however, the player will modify player.PlayerGui.Guis.AutoclickerOn.StopButtonPressed
as all instances in starter gui get cloned to playergui

local rem = game.ReplicatedStorage.AutoclcikerAdder

if Player.PlayerGui.Guis.AutoclickerOn.StopButtonPressed.Value == false then --// Your job to find out how to get the player.
	rem.OnServerEvent:Connect(function(player)
		player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1
	end)
else
	print("Stopped")
end

How am I supposed to do it tho as it is a serverscript

Hi, so personally, the best and easiest way to get the player from a server script is via any form of a function operating off of the player joining or an event ie; .Touched or RemoteEvent. As you’re using a remote event, I’d simply do it this way;

--// Server Script \\--
local rem = game.ReplicatedStorage.AutoclickerAdder

rem.OnServerEvent:Connect(function(player)
     if player.PlayerGui.Guis.AutoclickerOn.StopButtonPressed.Value == false then
          player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1
     else
          print("Stopped")
     end
end)

As this method finds you the player immediatly in the function parameters, and allows you to use it throughout your code. As it’s something operating with gui and only on the player who clicked the button, using player.PlayerGui is the best method to achieve this as the StarterGui would replicate for all players after spawn(s).

If this fails to function for you, best way to reach me is via discord @ BryanFehr#3070
There I can provide you with a further explanation and or examples or how I’ve achieved similar things in the past! :slight_smile:

1 Like

Thank you :))) I changed and added some things and it works now

1 Like

Just so you know that might cause a memory leak.
if someone uses an auto clicker the remote event will be fired 500 times a second.

I’d change the remote event to start the function rather than being within the function so you can define “.OnServerEvent(player)”

Edit: Sorry, didnt see the solution was already there :sweat_smile:

I have made an antiautoclicker, and btw this is a autoclicker gamepass

Thats not what im saying.
Auto clicker = fast right?
If you keep spamming the click then a memory leak is 100%
[Memory leak = your game is unplayable cause of the lag]

Do you mean the click button or smthin or are you telling you for the gamepass

The button. Constantly firing a remote event is 100% no doubt about it going to make your game unplayable.

Is that why it is showing this error today image

no thats just a glitch going around. nothing to worry about
roblox is doing some maintence

Ohk and the autoclicker thing you are saying , I get it now , but then how am I supposed to check when the off button is pressed ?

this will explain what im talking about

Yes I know what you are saying but then how will I make my code work

if your still confused make a seperate topic

make a cool down for clicking or use a complex garbage cleanup script
still confused? [Garbage Collection and Memory Leaks in Roblox - What you should know i found a tutorial

It has a cooldown of 1 second for clicking