Lua - 脚本化 - 为Roblox 战斗王者游戏创建一个箱子
2020-6-27 0:55:22
收藏:0
阅读:112
评论:1
我最近和朋友们一起开始制作我的第一个Roblox游戏。我正在编写某种盒子,所以当碰触它时会给你某种枪支。我尝试了一些随机调整,但它从未起作用。这是我的代码: 它的作用是等待玩家触摸它,然后执行动画(我已经将动画插入到脚本中),然后生成一个随机数。根据随机数,它确定给玩家什么。然后它会摧毁箱子,所以玩家无法反复获得该箱子。
math.randomseed(tick())
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
script.Disabled = true
local player = hit.Parent.Name
local num = 0
for i = 1,45 do
wait()
script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )
script.Parent.Part.CFrame = ( CFrame.new(script.Parent.Part.Position) * CFrame.Angles(0,math.rad(num),0) )
end
num = 0
num = math.random(0,100)
if num>-1 and num<5 then
local sword1 = game.ServerStorage.XM1404:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<10 then
local sword1 = game.ServerStorage.MP5:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<15 then
local sword1 = game.ServerStorage.SPAS12:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<20 then
local sword1 = game.ServerStorage.M9:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<25 then
local sword1 = game.ServerStorage.M4A1:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<30 then
local sword1 = game.ServerStorage.M32:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<35 then
local sword1 = game.ServerStorage.GC:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<44 then
local sword1 = game.ServerStorage.Riot Shield:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<60 then
local sword1 = game.ServerStorage.Grenade:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<65 then
local sword1 = game.ServerStorage.M60:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<70 then
local sword1 = game.ServerStorage.AUG:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<90 then
local sword1 = game.ServerStorage.PartHP:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<93 then
local sword1 = game.ServerStorage.RocketL:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<97 then
local sword1 = game.ServerStorage.Max Heal Potion:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<98 then
local sword1 = game.ServerStorage.DaggerOfShattereedDimensions:Clone()
sword1.Parent = game.Players[player].Backpack
elseif num<100 then
local sword1 = game.ServerStorage.Pulse:Clone()
sword1.Parent = game.Players[player].Backpack
end
script.Parent:Destroy()
end
end)
所有的枪支都在服务器存储中
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

确保你的脚本在部件内而不是模型内。Touched仅适用于单个部件,而不是模型。
另外,我不认为你将武器放入玩家背包的方式能够生效。请改用Roblox的GetPlayerFromCharacter方法,如下所示:
player = game.Players:GetPlayerFromCharacter(character) sword1.Parent = player.Backpack这种方法可以从角色中获取玩家,从而访问玩家的背包。关于该方法的更多信息,请参见这里:https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter
我还想建议你一种更简单的方法,它不需要那么多的if语句。我建议将所有枪放入ServerStorage文件夹中的文件夹中。你可以在脚本中为这个文件夹定义一个变量。然后创建另一个变量,使用Roblox的GetChildren()命令获取文件夹中的所有物品。这将返回所有文件夹内的枪的表。然后可以使用math.random来随机选择该表中的一项。
这个例子看起来是这样的:
local ServerStorage = game:GetService("ServerStorage") local gunsFolder = ServerStorage:WaitForChild("GunsFolder") local gunsTable = gunsFolder:GetChildren() local randomGun = gunsTable[math.random(1, #gunsTable)]--#获取表中元素个数我还想补充一点,在你作为初学者时,你可能会发现使用防抖比禁用脚本更有益。防抖仅仅是使用一个布尔变量和一个if语句来确保你的Touched事件仅在触摸之间的某个时间间隔后才发生。你可以使用防抖来确保玩家只能触摸一次箱子。这里有更多关于防抖的信息:https://developer.roblox.com/en-us/articles/Debounce
希望这有所帮助,如果你仍然有问题,请随时随地跟进。