如何从不同的本地脚本调用函数?

问题描述 投票:0回答:2

我有一个东西可以在玩家进入房间时治愈他们。我希望房间里的玩家在治疗时看到绿屏边框闪烁效果。我怎样才能实现这个目标?我尝试使用本地脚本,这样每个人都不会出现闪现效果,但不知道如何激活它,因为修复脚本是普通脚本,而闪屏是本地脚本。另外,如果有任何其他方法可以解决这个问题,而不是从另一个脚本激活功能,我会很高兴听到它。

这里有一些代码如果有帮助的话: 治愈脚本

desc = script.Parent.SurfaceGui.BlackBG.Desc
status = script.Parent.SurfaceGui.BlackBG.Status
bar = script.Parent.SurfaceGui.BlackBG.BarBG.Bar
barBG = script.Parent.SurfaceGui.BlackBG.BarBG
hitBox = script.Parent.Parent.Hitbox

local charge = 0
local MaxCharge = 100
local playersInBox = {}

status.Text = "ACTIVE"
status.TextColor3 = Color3.fromRGB(100, 255, 100)
desc.Text = "BIOREGENERATOR IS CURRENTLY ACTIVE AND OPERATIONAL. RESTORES HEALTH AND SANITY OF NEARBY HUMANS IN THIS ROOM."
desc.TextColor3 = Color3.fromRGB(0, 150, 0)

hitBox.Touched:Connect(function(hit)
    if hit.Name ~= "HumanoidRootPart" then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    if not table.find(playersInBox, player) then
        table.insert(playersInBox, player)
    else
        return
    end
end)

hitBox.TouchEnded:Connect(function(hit)
    if hit.Name ~= "HumanoidRootPart" then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    if table.find(playersInBox, player) then
        local num = table.find(playersInBox, player)
        table.remove(playersInBox, num)
    else
        return
    end
end)

while true do
    if charge ~= 100 then
        repeat
            charge = charge + 0.5
            bar.Size = UDim2.new(charge / MaxCharge, 0, 1, 0)
            wait()
        until charge == MaxCharge
    else
        for index, value in ipairs(playersInBox) do
            if not value.Character then table.remove(value) continue end
            local char = value.Character
            local human = char:FindFirstChild("Humanoid")
            local MaxSP = human:FindFirstChild("MaxSP")
            local SP = human:FindFirstChild("SP")
            human.Health += 12
            SP.Value += 12
            if SP.Value > MaxSP.Value then
                SP.Value = MaxSP.Value
            end
        end
        charge = 0
    end
    wait()
end

Flash脚本:

function flash()
    local healFlash = game.StarterGui.ScreenGui.HealFlash
    healFlash.ImageTransparency = 0
    repeat
        healFlash.ImageTransparency += 0.05
        wait()
    until healFlash.ImageTransparency == 1
end
roblox
2个回答
0
投票

所以这实际上很简单,将Flash脚本变成

ModuleScript
,然后在主脚本中需要它。

https://developer.roblox.com/en-us/api-reference/class/ModuleScript

FlashScript(主脚本的子级):

local module = {}

function module.flash()
    local healFlash = game.StarterGui.ScreenGui.HealFlash
    healFlash.ImageTransparency = 0
    repeat
        healFlash.ImageTransparency += 0.05
        wait()
    until healFlash.ImageTransparency == 1
end

return module

^ 在此代码片段中,我们需要将函数添加到表中,然后返回该表,以便在需要时可以访问它。如果没有 return 语句就会失败。

主要脚本:

local flashScript = require(script.FlashScript)
flashScript.flash()

desc = script.Parent.SurfaceGui.BlackBG.Desc
status = script.Parent.SurfaceGui.BlackBG.Status
bar = script.Parent.SurfaceGui.BlackBG.BarBG.Bar
barBG = script.Parent.SurfaceGui.BlackBG.BarBG
hitBox = script.Parent.Parent.Hitbox

local charge = 0
local MaxCharge = 100
local playersInBox = {}

status.Text = "ACTIVE"
status.TextColor3 = Color3.fromRGB(100, 255, 100)
desc.Text = "BIOREGENERATOR IS CURRENTLY ACTIVE AND OPERATIONAL. RESTORES HEALTH AND SANITY OF NEARBY HUMANS IN THIS ROOM."
desc.TextColor3 = Color3.fromRGB(0, 150, 0)

hitBox.Touched:Connect(function(hit)
    if hit.Name ~= "HumanoidRootPart" then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    if not table.find(playersInBox, player) then
        table.insert(playersInBox, player)
    else
        return
    end
end)

hitBox.TouchEnded:Connect(function(hit)
    if hit.Name ~= "HumanoidRootPart" then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    if table.find(playersInBox, player) then
        local num = table.find(playersInBox, player)
        table.remove(playersInBox, num)
    else
        return
    end
end)

while true do
    if charge ~= 100 then
        repeat
            charge = charge + 0.5
            bar.Size = UDim2.new(charge / MaxCharge, 0, 1, 0)
            wait()
        until charge == MaxCharge
    else
        for index, value in ipairs(playersInBox) do
            if not value.Character then table.remove(value) continue end
            local char = value.Character
            local human = char:FindFirstChild("Humanoid")
            local MaxSP = human:FindFirstChild("MaxSP")
            local SP = human:FindFirstChild("SP")
            human.Health += 12
            SP.Value += 12
            if SP.Value > MaxSP.Value then
                SP.Value = MaxSP.Value
            end
        end
        charge = 0
    end
    wait()
end

^ 第 1 行加载模块脚本,然后您可以将第 2 行移动到代码中需要的位置。第 2 行调用该函数。您现在可以使用参数和返回值,就像该函数位于主脚本中一样。

如果您需要更多帮助,请回复:)

--哈维


0
投票

这有效!这对我有很大帮助。

谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.