Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I don't understand why functions are used can someone explain?

Asked by 6 years ago

I really don't understand why you do this function lightOnFire() print("Lighting on fire!") end with functions instead of just

print("Lighting on fire!")

0
a function is when you input something and expect an output, for example local function (input) if input == true then print("im true!") elseif input == false then print("im false!") AttentionHog 70 — 6y

1 answer

Log in to vote
0
Answered by
arshad145 392 Moderation Voter
6 years ago
Edited 6 years ago

Hello,

This is a bit advanced , but it is a good explanation.

function hookmake ()
    local hook = Instance.new("Part")
    hook.Name = "Projectile"
    hook.Anchored = false
    hook.CanCollide = false 
    hook.Transparency = 0
    hook.BrickColor = BrickColor.new("Dark stone grey")
    hook.Material = "Metal"
    hook.Size = Vector3.new(0.75,0.4,0.4)
    local hookmesh = Instance.new("SpecialMesh")
    hookmesh.MeshId = "rbxassetid://1321670738"
    local hookatach = Instance.new("Attachment")
    hookatach.Name = "RopeAttachment"
    hookatach.Visible = false
    hook.Parent = game.Lighting
    hookmesh.Parent = hook
    hookatach.Parent = hook
    hookatach.Position = Vector3.new(0,0,0.3)
    return hook,hookmesh,hookatach
end -- This is a function I made.

My question is would you write all that everytime you need the specific instance or just call the function?

The answer is obviously you will call the function everytime you need it.

hookmake()

In your case, it is a simple script.

function lightOnFire() 
    print("Lighting on fire!") 
end

The answer is obvious you would not want to write print("Lighting on fire!") everytime you need the desired ouput. Just by calling the function it will output. lightOnFire()

Function is a way to make life easier. In life everything has a function and it is the same in programming.

Thank you for reading.

Feel free to ask any questions.

PS : Your other solution.

t = 0 -- time
while true do wait(.1)
    t = t + 1 -- 1 hour in the game is 1 minute in the real life.
    game.Lighting:SetMinutesAfterMidnight(t) -- Keeps the time cycle going on.

    if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then -- This is 1080 minutes.
        print(game.Lighting:GetMinutesAfterMidnight())
    end
    if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then -- This is 360 minutes.
        print(game.Lighting:GetMinutesAfterMidnight())
    end
end
0
Thx Lukas4306 4 — 6y
0
I made a street light script but it doesn't work here if game.Lighting:GetMinutesAfterMidnight()== 6*60 then script.Parent.Material=Enum.Material.Plastic script.Parent.PointLight.Enabled=false end if game.Lighting:GetMinutesAfterMidnight()== 18*60 then script.Parent.Material=Enum.Material.Neon script.Parent.PointLight.Enabled=true end Lukas4306 4 — 6y
0
Replace the prints. arshad145 392 — 6y
0
Ok so I made a new script which is T=0 while true do T=T+10 game.Lighting:SetMinutesAfterMidnight(T) wait(.1) if game.Lighting:GetMinutesAfterMidnight()== 6*60 then script.Parent.Material=Enum.Material.Plastic script.Parent.PointLight.Enabled=false end if game.Lighting:GetMinutesAfterMidnight()== 18*60 then script.Parent.Material=Enum.Material.Plastic script.Parent.P Lukas4306 4 — 6y
Ad

Answer this question