Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

5 comments

0

Just one question. Is anywhere in the script, are you defining 'running'?

0

Mhm, I posted the full script just now.

0

You should call the function, when he starts running

0

I will try that, thank you

0

What exactly would that look like?

2 answers

✓ Accepted answer

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.

2 comments

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

0

Oh wow! You guys are really helpful! Thanks! :D

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

1 comment

0

It didn't work, but thank you for attempting to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.