【问题标题】:Lua - scripting - for a roblox battle royale game crateLua - 脚本 - 用于 roblox 大逃杀游戏箱
【发布时间】:2020-10-17 09:53:26
【问题描述】:

我最近开始在 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)

所有枪支都在服务器存储中

【问题讨论】:

  • 对不起,但它不起作用:(
  • “不起作用”是什么意思?期望的结果是什么?会发生什么?
  • 期望的结果是箱子应该给玩家(触摸箱子)随机生成的枪然后消失。发生的情况是,当玩家触摸它时,什么也没有发生。它就像一个用于装饰的道具/盒子。
  • 这些东西都是Tools吗?
  • 是的,我在问之前确定了。

标签: lua scripting roblox


【解决方案1】:

确保您的脚本位于零件而不是模型中。 Touched 仅适用于单个零件,不适用于模型。

此外,我不相信你将武器插入玩家背包的方式会起作用。而不是 Sword1.Parent = game.Players[player].Backpack,尝试使用 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)]--# gets number of elements in a table

我想补充的另一个可能对初学者有帮助的注意事项是使用 debounce 而不是禁用脚本。 Debounce 只是使用布尔变量和 if 语句来确保您的 Touched 事件仅在两次触摸之间的特定时间间隔后发生您可以使用 debounce 来确保玩家只能触摸板条箱一次。以下是关于去抖动的更多信息:https://developer.roblox.com/en-us/articles/Debounce

希望这会有所帮助,如果您仍有问题,请随时跟进。

【讨论】:

  • 非常感谢。我会尝试看看这是否有效。我已经尝试解决这个问题 3 周了。 :)
猜你喜欢
  • 2020-11-08
  • 2018-11-26
  • 2020-03-12
  • 2018-12-01
  • 2020-07-02
  • 2021-03-25
  • 2016-03-31
  • 1970-01-01
  • 2022-10-20
相关资源
最近更新 更多