This has been a pain... please help me with this annoying script

Now I have actually been trying to see out the issue in this script:

local inputServ = game:GetService("UserInputService")
local tweenServ = game:GetService("TweenService")
local dist = 5
local text = script.Parent.Parent.Parent.e
local part = workspace.Thing
local lights = false
local canDo = false
local tweenInf = TweenInfo.new(1)
local tween = tweenServ:Create(script.Parent, tweenInf, {Size = UDim2.new(1, 0, 1, 0)})

local e = coroutine.create(function()
	while wait() do
		if (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
			text.Visible = true
			if lights then
				text.Text = "<E> to turn off lights"
			elseif lights == false then
				text.Text = "<E> to turn on lights."
			end
		else
			text.Visible = false
		end
	end
end)

local er = coroutine.create(function()
	while wait() do
		canDo = inputServ:IsKeyDown(Enum.KeyCode.E)
	end
end)

coroutine.resume(e)

coroutine.resume(er)

inputServ.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E and (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
		game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = true
		script.Parent.Parent.Visible = true
		tween:Play()
		wait(1)
		if canDo == false then
			return
		elseif canDo == true then
			if lights then
				print("Lights are now off")
				workspace.Thing.PointLight.Enabled = false
				lights = false
			elseif lights == false then
				print("Lights are now on")
				workspace.Thing.PointLight.Enabled = true
				lights = true
			end
		end
		tween:Cancel()
		script.Parent.Parent.Visible = false
		script.Parent.Size = UDim2.new(0, 0, 1, 0)
		game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = false
	end
end)

inputServ.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		tween:Cancel()
		script.Parent.Parent.Visible = false
		script.Parent.Size = UDim2.new(0, 0, 1, 0)
		game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = false
	end
end)

(LocalScript ^^^)

And I seriously can’t fix this. Here is the issue, when the player holds E and lets go, when they re-hold E, it will toggle the lights before the next bar has reached the end. I hope that makes sense.

Here is a video: Dropbox - File Deleted - Simplify your life

1 Like

Please, someone help me with this :frowning:

Try changing this section to true.

2 Likes

No it won’t make a change. What I am saying is when I hold E, if I stop holding E within let’s say something like .4 seconds in, if I let go and re-hold E then it will minus .4 off the time and toggles the lights and then the current bar toggles the lights again unless I don’t hold E until the time has finished. I bet this makes no sense, I am sorry, it’s so hard to explain :sob:

You can test out the game here: Lights - Roblox

When you hold E, let go of it after .4 seconds then hold E again then you should see what I mean. Make sure to get close to the lights.

It seems to work perfectly fine on the game you linked.

Try playing it again.

Let me send another video. Hang on

Here is the video: Dropbox - File Deleted - Simplify your life

Ah I see, it only does this when you come to close to the light switch?

1 Like

Well you need to be close to interact with it. So kinda

Ok I think I found the solution. The script is registering every time the player presses E. lets say you have to hold down E for 5 seconds for the lights to turn off. If you hold E for 1 second and then release the E key then followed by pressing E again you will wait 4 seconds rather than 5, the reason being the script only end’s when you hold down the E key for 5 seconds.

local inputServ = game:GetService("UserInputService")
local tweenServ = game:GetService("TweenService")
local dist = 5
local text = script.Parent.Parent.Parent.e
local part = workspace.Thing
local lights = false
local canDo = false
local tweenInf = TweenInfo.new(1)
local tween = tweenServ:Create(script.Parent, tweenInf, {Size = UDim2.new(1, 0, 1, 0)})

local e = coroutine.create(function()
	while wait() do
		if (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
			text.Visible = true
			if lights then
				text.Text = "<E> to turn off lights"
			elseif lights == false then
				text.Text = "<E> to turn on lights."
			end
		else
			text.Visible = false
		end
	end
end)

local er = coroutine.create(function()
	while wait() do
		canDo = inputServ:IsKeyDown(Enum.KeyCode.E)
	end
end)

coroutine.resume(e)

coroutine.resume(er)



local dbg = true
inputServ.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E and (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
		dbg = true
		game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = true
		script.Parent.Parent.Visible = true
		tween:Play()
		wait(1)
		if not dbg then
			return
		end
		if canDo == false then
			return
		elseif canDo == true then
			if lights and dbg then
				print("Lights are now off")
				workspace.Thing.PointLight.Enabled = false
				lights = false
			elseif not lights dbg then
				print("Lights are now on")
				workspace.Thing.PointLight.Enabled = true
				lights = true
			end
		end
		if not dbg then
			return
		end
		tween:Cancel()
		script.Parent.Parent.Visible = false
		script.Parent.Size = UDim2.new(0, 0, 1, 0)
		game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = false
	end
end)

inputServ.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		dbg = false
		tween:Cancel()
		script.Parent.Parent.Visible = false
		script.Parent.Size = UDim2.new(0, 0, 1, 0)
		game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = false
	end
end)

Give this a try?

2 Likes

elseif not lights dbg then am I supposed to put and before dbg?

Edit: Yep

My bad… I just put it into a notepad and went from there, It would help if you could upload a studio file

Testing right now, please wait…

Doesn’t work, it still does the same thing, here is the file: Lights.rbxl (2GB)

local inputServ = game:GetService("UserInputService")
local tweenServ = game:GetService("TweenService")
local dist = 5
local text = script.Parent.Parent.Parent.e
local part = workspace.Thing
local lights = false
local canDo = false
local tweenInf = TweenInfo.new(1)
local tween = tweenServ:Create(script.Parent, tweenInf, {Size = UDim2.new(1, 0, 1, 0)})

spawn(function()
	while wait() do
		if (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
			text.Visible = true
			if lights then
				text.Text = "<E> to turn off lights"
			elseif lights == false then
				text.Text = "<E> to turn on lights."
			end
		else
			text.Visible = false
		end
	end
end)






local LastStart = tick()
tween.Completed:Connect(function()
	if tick() - LastStart > 0.6 then
		print("Toggled Lights")
		lights = not lights
		workspace.Thing.PointLight.Enabled = not workspace.Thing.PointLight.Enabled
	end
	LastStart = tick()
end)


function Cancel()
	script.Parent.Parent.Visible = false
	tween:Cancel()
	script.Parent.Size = UDim2.new(0, 0, 1, 0)
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = false
end


function Start()
	script.Parent.Size = UDim2.new(0, 0, 1, 0)
	LastStart = tick()
	tween:Play()
	script.Parent.Parent.Visible = true
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = true
end


inputServ.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E and (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
		Start()
	end
end)

inputServ.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		Cancel()
	end
end)

Works for me, might need to change the timing a bit

Ah, another issue.

Here is the issue (in a video): Dropbox - File Deleted - Simplify your life

Where do I put the elseif?

local inputServ = game:GetService("UserInputService")
local tweenServ = game:GetService("TweenService")
local dist = 5
local text = script.Parent.Parent.Parent.e
local part = workspace.Thing
local lights = false
local canDo = false
local tweenInf = TweenInfo.new(1)
local tween = tweenServ:Create(script.Parent, tweenInf, {Size = UDim2.new(1, 0, 1, 0)})


local LastStart = tick()


spawn(function()
	while wait() do
		if tick() - LastStart > 1 then
			Cancel()
		end
		if (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
			text.Visible = true
			if lights then
				text.Text = "<E> to turn off lights"
			elseif lights == false then
				text.Text = "<E> to turn on lights."
			end
		else
			text.Visible = false
		end
	end
end)



function Cancel()
	LastStart = tick()
	script.Parent.Parent.Visible = false
	tween:Cancel()
	script.Parent.Size = UDim2.new(0, 0, 1, 0)
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = false
end




tween.Completed:Connect(function()
	if tick() - LastStart > 0.6 and tick() - LastStart < 1 then
		print("Toggled Lights")
		lights = not lights
		workspace.Thing.PointLight.Enabled = not workspace.Thing.PointLight.Enabled
	end
	LastStart = tick()
end)


function Start()
	script.Parent.Size = UDim2.new(0, 0, 1, 0)
	LastStart = tick()
	tween:Play()
	script.Parent.Parent.Visible = true
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Anchored = true
end


inputServ.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E and (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude < dist then
		Start()
	end
end)

inputServ.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		Cancel()
	end
end)
1 Like

Thanks, this worked but there is a tiny delay before letting the player go and hiding the UI.