im making a roblox game and need to update the leaderboard stats when you click the baseplate

211 Views Asked by At

this is my code at the moment:

local players = game:WaitForChild("Players")
    
local function createLeaderboard(player)
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    local baseclicks = Instance.new("IntValue", stats)
    baseclicks.Name = "baseclicks"
    stats.Parent = player
    baseclicks.Value = 100
end
   
players.PlayerAdded:connect(createLeaderboard)

Im not sure if i need a clickdetector with a script inside or?? i dont know, please help.

1

There are 1 best solutions below

0
On

If you don't want ClickDetectors on the Baseplate, simply, you could use mouse.Target. For example:

-- serverscript
local players = game:GetService("Players");

local function createLeaderboard(player);
  local stats = Instance.new("Folder", player);
  stats.Name = "leaderstats";
  local baseclicks = Instance.new('IntValue', stats);
  baseclicks.Name = 'baseclicks'
  baseclicks.Value = 100;
end
-- localscript in startercharacterscript

local players = game:GetService("Players");
local client = players.LocalPlayer;
local mouse = client:GetMouse(); --method for getting the client's mouse
local event = game.ReplicatedStorage.OnClick --our remote event

local leaderstats = client.leaderstats or client:WaitForChild("leaderstats");
local clickvalue = leaderstats.baseclicks or leaderstats:WaitForChild("baseclicks");

mouse.Button1Down:Connect(function()
  if not (mouse.Target) then return; end

  if (mouse.Target.Name == "Baseplate") then
    event:FireServer(clickvalue.Value + 1); --fire the remote event
  end
  end
end)
-- server script in serverscriptservice for remote event receiver
game.ReplicatedStorage.OnClick.OnServerEvent:Connect(function(Player, Value)
  Player.leaderstats.baseclicks.Value = Value;
end

However, when it comes to the game having multiple players clicking at once, remote events are not recommended as they are more than likely going to lag the server or cause network traffic - you're better off using ClickDetectors.