我没有太多的经验,所以我来这里寻求帮助。我目前正在制作一个名为ROBLOX的游戏脚本,但我在我的脚本中遇到了一个问题,它来自这个小部分
me.Chatted:connect(function(msg)
if string.sub(msg,1,5) == "!kick" then
local PLAYER = (''.. string.sub(msg,6))
KICK('game.Players.PLAYER')
end
end)
(我得到的错误是:缺少参数1或者缺少)
我对于该做什么感到很遗憾,但这是我脚本的其余部分..
local me = game.Players.LocalPlayer
function KICK(PLAYER)
spawn(
function()
local function SKICK()
if
PLAYER.Character
and PLAYER.Character:FindFirstChild('HumanoidRootPart')
and PLAYER.Character:FindFirstChild('Torso')
then
local SP = Instance.new('SkateboardPlatform', PLAYER.Character)
SP.Position = Vector3.new(1000000, 1000000, 1000000)
SP.Transparency = 1
PLAYER.Character.HumanoidRootPart.CFrame = SP.CFrame
PLAYER.Character.Torso.Anchored = true
end
end
spawn(
function()
repeat
wait()
if PLAYER ~= nil then
SKICK()
end
until not game:GetService('Players'):FindFirstChild(PLAYER.Name)
if not game:GetService('Players'):FindFirstChild(PLAYER.Name) then
print('REMOVED ' .. PLAYER.Name)
end
end
)
end
)
end
me.Chatted:connect(function(msg)
if string.sub(msg,1,5) == "!kick" then
local PLAYER = (''.. string.sub(msg,6))
KICK('game.Players.PLAYER')
end
end)
答案 0 :(得分:1)
在代码的这一部分:
local PLAYER = (''.. string.sub(msg,6))
KICK('game.Players.PLAYER')
在更改代码时,您似乎犯了一些语法错误。这是它应该是什么样子:
local PLAYER = string.sub(msg,6)
KICK('game.Players.' .. PLAYER)
除此之外也没有意义。您正在将字符串'game.Players.' .. PLAYER
传递给函数KICK()
,但KICK()
使用其参数PLAYER
,就好像它是一个Player对象,如使用PLAYER.Character
时所见和PLAYER.Name
。您传入一个字符串并尝试像Player对象一样使用它。
解决此问题的一种方法是将实际的Player对象传递给KICK()
而不是String。例如:
local PLAYER = string.sub(msg,6)
KICK(game:GetService('Players'):FindFirstChild('game.Players.' .. PLAYER))
此修订版找到与用户名'game.Players.' .. PLAYER
对应的Player对象,然后将 传递给KICK()
。现在,虽然这会在您的代码中修复一个更明显的问题,但它似乎并不能解决您引用的问题,“缺少参数1或缺少”,但您永远不会知道。如果您进行了这些更改,它是否正常工作?
答案 1 :(得分:0)
您正在传递一个字符串,并试图为其命名,这是不好的。
您应将KICK('game.Players.PLAYER')
更改为KICK(game:GetService("Players")[PLAYER])
您要将播放器对象/用户数据传递给函数而不是字符串。
由于“ game.Players.PLAYER”。名称为nil,错误提示“参数丢失或为nil”
答案 2 :(得分:0)
(对不起,请稍后回复)
我在此脚本上看到的第一个问题是,它是一个LocalScript,或者至少是它的外观,因为您做了game.Players.LocalPlayer
。问题在于,LocalScript根本无法踢球员,也不会以任何方式伤害他们。这就是为什么大多数管理员/踢命令使用服务器端系统的原因,换句话说,使用脚本而不是LocalScript。
我强烈建议您将其转换为ModuleScript或Script,这样您可以更轻松地使用它。你知道吗?我会帮你的,所以不用担心。
因此,首先,我需要知道您的ROBLOX名称。假设是John Doe,现在,如果您的用户名不同(这肯定很有可能),那么您只需将John Doe更改为您的ROBLOX用户名即可。
让我们开始吧!
乍一看有时听起来很困难,但确实如此,但是由于此脚本非常简单,所以没问题。
首先,在名为“ MainModule”的ModuleScript中,粘贴此代码(如果不是John Doe,则将John Doe更改为您的ROBLOX用户名。)
请注意,在诸如脚本生成器之类的地方,甚至在您自己的游戏中,使用ModuleScript都是非常有用的!
还请注意,此ModuleScript要正常运行,只能通过脚本运行,而不能通过LocalScript运行。
local username = "John Doe";
local connected = false;
local Players = game:service("Players");
local p = nil;
function kick(name)
for _,v in pairs(Players:GetPlayers())do
if v.Name == name then
v:Kick("You've been kicked.");
end;
end;
end;
for _,v in pairs(Players:GetPlayers())do
if v.Name == username then
p = v;
end;
end;
Players.PlayerAdded:connect(function(plr)
if plr.Name == username then
p = plr;
end;
end;
Players.PlayerRemoving:connect(function(plr)
if plr.Name == username then
p = nil;
connected = false;
end;
end;
coroutine.wrap(function()
while wait()do
local r,e = ypcall(function()
if p ~= nil then
if not connected then
connected = true;
p.Chatted:connect(function(msg)
if msg:sub(1,string.len("kick"))then
local other = tostring(msg:sub(string.len("kick ")));
local search = other:lower();
kick(search);
end;
end);
end;
end;
end);
if e then
warn(":ERROR: "..tostring(e));
end;
end;
end)();
return nil;
那应该为模块做! 要执行此代码,您必须执行以下步骤:
首先,在Workspace中创建一个ModuleScript(在Roblox Studio中完成),现在,在ModuleScript中,粘贴我刚刚编写的代码。现在,将该ModuleScript从“ ModuleScript”重命名为“ MainModule”。现在像发布模型一样发布此脚本。
在那之后,您将看到脚本具有一个ID(例如12345678)。该ID位于模型的URL中。该ID是您所需要的。复制该ID,然后在这个小脚本require(<enter ID here, replacing me>);
中将ID粘贴到Parentesys之间。它看起来应该像require(12345678);
。
如果您愿意在脚本生成器中执行kick脚本,您要做的就是在聊天室或命令栏中键入以下内容:c/
+ require();
I告诉你去做。它看起来应该像c/require(12345678);
。之后,只需按Enter,您的kick命令脚本将开始运行!踢球员所需要做的只是说“踢”,然后说出他们的名字!请注意,这不区分大小写,但是您必须写出玩家的全名。
示例:
如果您在运行该模块后在游戏聊天中说了这些示例中的任何一个,脚本将尝试踢一个名为“ LegendOfDarknees”的玩家(就是我!),但是,如果该玩家不在服务器上,脚本不会做任何事情。
此外,请注意,脚本将永远在服务器中运行! (或直到服务器关闭)。这意味着您可以尝试踢自己或重新加入,仍然可以踢球员!
(请不要滥用此内容)
执行此操作的方法有两种,一种方法是与我在将其转换为ModuleScript 中所述的方法相同,您要做的就是在Workspace中创建一个Script,在脚本中,只需输入require(<ID of the module>);
。它看起来应该像require(12345678);
。该脚本将自动执行该模块,因此您无需手动激活它。
现在,有另一种方法不是我最喜欢的方法,因为对于开发人员而言,更容易获得此脚本。
为此,您要做的就是在Workspace中创建一个Script。现在,在脚本内部,粘贴以下代码:
local username = "John Doe";
local connected = false;
local Players = game:service("Players");
local p = nil;
function kick(name)
for _,v in pairs(Players:GetPlayers())do
if v.Name == name then
v:Kick("You've been kicked.");
end;
end;
end;
for _,v in pairs(Players:GetPlayers())do
if v.Name == username then
p = v;
end;
end;
Players.PlayerAdded:connect(function(plr)
if plr.Name == username then
p = plr;
end;
end;
Players.PlayerRemoving:connect(function(plr)
if plr.Name == username then
p = nil;
connected = false;
end;
end;
coroutine.wrap(function()
while wait()do
local r,e = ypcall(function()
if p ~= nil then
if not connected then
connected = true;
p.Chatted:connect(function(msg)
if msg:sub(1,string.len("kick"))then
local other = tostring(msg:sub(string.len("kick ")));
local search = other:lower();
kick(search);
end;
end);
end;
end;
end);
if e then
warn(":ERROR: "..tostring(e));
end;
end;
end)();
它看起来与模块一非常相似,因为它们是相同的代码,唯一的区别是该模块最后没有做return nil;
。这是因为在强制执行ModuleScript的同时,该脚本不必将某些内容返回执行脚本的脚本,否则,ModuleScript将不会运行。
因此,应该这样做来回答您的问题。希望您能读完所有这些内容,因为这是一个很长的文本,阅读冗长的文本可能看起来很无聊或沮丧,但是这次,它充满了重要的信息,因此我强烈建议您阅读。
再次,很抱歉这么晚回复,我之前无法回复。我希望我还不晚。
如果您在通过此答案发送给您的代码中遇到任何错误/小故障/错误,请随时在此答案的评论中告诉我,如果您需要更多信息,我可以编辑答案,或者对您的评论进行注释发布给您更多信息。希望这能回答您所有的问题,还有更多答案。
祝您当前和未来的项目好运!