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.
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
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)
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
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)