'How to call function from a different local script?
I have a thing that heals the player if they get into the room. And I want to players who are in the room to see a green screen borders flash effect upon healing. How can I achieve this? I tried to use local script so that everyone won't have the flash effect but don't know how to activate it since healing script is a normal script while flash screen is a local script. Also, if there are any other ways of solving this problem rather than activating function from another script, I will be glad to hear it.
Here is some code if this will help: Heal script
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 script:
function flash()
local healFlash = game.StarterGui.ScreenGui.HealFlash
healFlash.ImageTransparency = 0
repeat
healFlash.ImageTransparency += 0.05
wait()
until healFlash.ImageTransparency == 1
end
Solution 1:[1]
So this is actually quite simple, turn the Flash script into a ModuleScript
and then require it in main script.
https://developer.roblox.com/en-us/api-reference/class/ModuleScript
FlashScript (Child of Main script):
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
^ In this code snippet we need to add the function to a table and then return that table so it can be accessed when required. If there isn't a return statement it will fail.
Main Script:
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
^ Line 1 loads the module script and then you can move line two to where ever you need it in the code. Line 2 calls the function. You can now use arguments and return values as if the function was in the main script.
If you need any more help, just reply :)
-- Harvey
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Harvey |