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

Am I scripting local functions and events correctly in this script?

Asked by 8 years ago

I started scripting around two months ago and decided to start branching out. For my first task, I decided to make a run button, and when the player runs, the game's brightness would increase, however, I need help, as it is not working and no errors are appearing in the output.

l--Player--
local Player = game.Players.LocalPlayer
local character = Player.Character
if not character or not character.Parent then
    character = Player.CharacterAdded:wait()
end

--Services/Other--
local mouse = game.Players.LocalPlayer:GetMouse()

--Variables--
local Head = character["Head"]
local LeftArm = character["Left Arm"]
local LeftLeg = character["Left Leg"]
local RightArm = character["Right Arm"]
local RightLeg = character["Right Leg"]
local Torso = character.Torso

--Functions--
function getTool()  
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end

mouse.KeyDown:connect(function (key)
    key = string.lower(key)
    if string.byte(key) == 107 then
        running = true
        local keyConnection = mouse.KeyUp:connect(function (key)
            if string.byte(key) == 108 then
                running = false 
            end
        end)
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
    game:GetService("Chat"):Chat(Head, "Clock Up...", Enum.ChatColor.Red)
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 150
        game.Players.LocalPlayer.Character.Humanoid.JumpPower = 65
        LeftLeg.BottomSurface = "Glue"
        RightLeg.BottomSurface = "Glue"
        repeat wait(99999999999999999999999999999999999999999) until running == false
        keyConnection:disconnect()
game:GetService("Chat"):Chat(Head, "Clock Over...", Enum.ChatColor.Red)
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
        game.Players.LocalPlayer.Character.Humanoid.JumpPower = 50
        LeftLeg.BottomSurface = "Smooth"
        RightLeg.BottomSurface = "Smooth"
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
            wait()
        end
    end
end)

--Local Functions--

function brightUp()
    if running == true then
        game.Lighting.Brightness = 2
        if running == false then
            game.Lighting.Brightness = 1
        end
    end
end

game.Lighting.LightingChanged:connect(brightUp)


If more code is needed, just ask!

0
Just one question. Is anywhere in the script, are you defining 'running'? Nickoakz 231 — 8y
0
Mhm, I posted the full script just now. Strykebyte 45 — 8y
0
You should call the function, when he starts running xuefei123 214 — 8y
0
I will try that, thank you Strykebyte 45 — 8y
0
What exactly would that look like? Strykebyte 45 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

Well if you make the script wait 99999999999999999999999999999999999999999 until it's allowed to check if running == false then you're gonna have to wait 99999999999999999999999999999999999999999 seconds before it prints anything too!

repeat wait(99999999999999999999999999999999999999999) until running == false
--If you're just yielding until running == false you should drop that timer to something smaller...

Also, you can have KeyUp events to turn running off instead of waiting for disconnect events, it's how I do my own sprinting systems.

0
Yea, the "until running == false" part of your repeat loop will NOT be checked until the massive wait is over, Just do repeat wait() until running == false, it will still stop the script until running has stopped alphawolvess 1784 — 8y
0
Oh wow! You guys are really helpful! Thanks! :D Strykebyte 45 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago
local function brightUp()
    if running == true then
        game.Lighting.Brightness = 2
        if running == false then
            game.Lighting.Brightness = 1
        end
    end
end

brightUp()

0
It didn't work, but thank you for attempting to help. Strykebyte 45 — 8y

Answer this question