制作 Roblox 游戏,需要在单击 baseplate 时更新排行榜统计数据

目前这是我的代码:

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)

我不确定是否需要一个带有脚本的 clickdetector 或其他什么东西? 我不知道,请帮帮我。

点赞
用户14373962
用户14373962

如果您不希望在基础板上使用ClickDetector,您可以使用mouse.Target。例如:

-- 服务器脚本
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
-- startercharacterscript中的本地脚本

local players = game:GetService("Players");
local client = players.LocalPlayer;
local mouse = client:GetMouse(); -- 获取客户端的鼠标
local event = game.ReplicatedStorage.OnClick -- 远程事件

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); -- 触发远程事件
  end
  end
end)
-- serverscriptservice中的服务器脚本用于接收远程事件
game.ReplicatedStorage.OnClick.OnServerEvent:Connect(function(Player, Value)
  Player.leaderstats.baseclicks.Value = Value;
end

但是,当游戏中有多个玩家同时点击时,不建议使用远程事件,因为它们很可能会使服务器延迟或导致网络流量增加 - 您最好使用ClickDetector

2020-10-08 07:49:02