Lua - 脚本化 - 为Roblox 战斗王者游戏创建一个箱子

我最近和朋友们一起开始制作我的第一个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)

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

点赞
用户13788973
用户13788973

确保你的脚本在部件内而不是模型内。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

希望这有所帮助,如果你仍然有问题,请随时随地跟进。

2020-07-01 03:00:56