Function not working

Prompt.Triggered:Connect(function(player)
	
	if IsHiding then return end
	--
	Prompt.Enabled = false
	local Character = player.Character
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	
	LeftDoorOpenTween:Play()
	RightDoorOpenTween:Play()
	wait(TweenTime)
	HumanoidRootPart.Position = PositionPart.Position -- I do get teleported
	LeftDoorCloseTween:Play()
	RightDoorCloseTween:Play()
	
	UIS.InputBegan:Connect(function(player) -- doesn't run
		print("InputBegan") -- doesn't print
		if UIS.InputBegan == Enum.KeyCode.W or UIS.InputBegan == Enum.KeyCode.E or UIS.InputBegan == Enum.KeyCode.S or UIS.InputBegan == Enum.KeyCode.D or UIS.InputBegan == Enum.KeyCode.Space then
			if IsHiding then
				print("KeyCode was Inputted") -- doesn't print
				LeftDoorOpenTween:Play()
				RightDoorOpenTween:Play()
				HumanoidRootPart.Position = PromptPart.Position
				IsHiding = false
				Prompt.Enabled = true
				wait(TweenTime)
				LeftDoorCloseTween:Play()
				RightDoorCloseTween:Play()
			end
		end	
			
	end)
	
	wait(Cooldown)
	print("Waited Cooldown") -- this prints
	
	if IsHiding then -- doesn't run
		
		LeftDoorOpenTween:Play()
		RightDoorOpenTween:Play()
		HumanoidRootPart.Position = PromptPart.Position
		print("Evicted") -- doesn't print
		IsHiding = false
		Prompt.Enabled = true
		wait(TweenTime)
		LeftDoorCloseTween:Play()
		RightDoorCloseTween:Play()
		
	end
	
end)

I don’t know why the UIS.InputBegan function is not running, and the two if IsHiding then functions either.

If you need more information, just ask!
Any help is appreciated!

Use task.wait(), wait() is deprecated.

Can you show the full script so I can see everything is defined correctly? Also, are there any errors in console?

task.wait() didn’t seem to do anything. And there are no errors in console.

Here is the full script:

local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

local PromptPart = script.Parent
local Prompt = PromptPart.ProximityPrompt
local Wardrobe = PromptPart.Parent
local LeftDoor = Wardrobe.LeftPivotPart
local RightDoor = Wardrobe.RightPivotPart
local PositionPart = Wardrobe.PositionPart
local Prompt = PromptPart.ProximityPrompt

local IsHiding = false
local Cooldown = 10
local TweenTime = 0.25
-- TWEENS
local DoorInfo = TweenInfo.new(TweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0)

local LeftDoorOpenProps = {CFrame = LeftDoor.CFrame * CFrame.Angles(0, math.rad(80), 0)}
local RightDoorOpenProps = {CFrame = RightDoor.CFrame * CFrame.Angles(0, math.rad(-80), 0)}
local LeftDoorCloseProps = {CFrame = LeftDoor.CFrame * CFrame.Angles(0, math.rad(0), 0)}
local RightDoorClseProps = {CFrame = RightDoor.CFrame * CFrame.Angles(0, math.rad(0), 0)}

local LeftDoorOpenTween = TS:Create(LeftDoor, DoorInfo, LeftDoorOpenProps)
local RightDoorOpenTween = TS:Create(RightDoor, DoorInfo, RightDoorOpenProps)
local LeftDoorCloseTween = TS:Create(LeftDoor, DoorInfo, LeftDoorCloseProps)
local RightDoorCloseTween = TS:Create(RightDoor, DoorInfo, RightDoorClseProps)
-- FUNCTIONS
Prompt.Triggered:Connect(function(player)
	
	if IsHiding then return end
	--
	Prompt.Enabled = false
	local Character = player.Character
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	
	LeftDoorOpenTween:Play()
	RightDoorOpenTween:Play()
	task.wait(TweenTime)
	HumanoidRootPart.Position = PositionPart.Position
	LeftDoorCloseTween:Play()
	RightDoorCloseTween:Play()
	
	UIS.InputBegan:Connect(function(player)
		print("InputBegan")
		if UIS.InputBegan == Enum.KeyCode.W or UIS.InputBegan == Enum.KeyCode.E or UIS.InputBegan == Enum.KeyCode.S or UIS.InputBegan == Enum.KeyCode.D or UIS.InputBegan == Enum.KeyCode.Space then
			if IsHiding then
				print("KeyCode was Inputted")
				LeftDoorOpenTween:Play()
				RightDoorOpenTween:Play()
				HumanoidRootPart.Position = PromptPart.Position
				IsHiding = false
				Prompt.Enabled = true
				task.wait(TweenTime)
				LeftDoorCloseTween:Play()
				RightDoorCloseTween:Play()
			end
		end	
			
	end)
	
	task.wait(Cooldown)
	print("Waited Cooldown")
	
	if IsHiding then
		
		LeftDoorOpenTween:Play()
		RightDoorOpenTween:Play()
		HumanoidRootPart.Position = PromptPart.Position
		print("Evicted")
		IsHiding = false
		Prompt.Enabled = true
		task.wait(TweenTime)
		LeftDoorCloseTween:Play()
		RightDoorCloseTween:Play()
		
	end
	
end)

task.wait() isn’t the fix. It will avoid issues in the future, deprecated features should always be avoided.
You seem to have some repeating code. You can clean up your code using a function that will create a coroutine to tween the doors while the rest of your code does it’s work.

local function animateDoors()
    coroutine.wrap(function()
        LeftDoorOpenTween:Play()
	    RightDoorOpenTween:Play()
	    task.wait(TweenTime)
	    LeftDoorCloseTween:Play()
	    RightDoorCloseTween:Play()
    end)()
end

This code will create a coroutine to animate the doors and immediately run it. If you didn’t use the coroutine, the script would wait for the doors to animate first before continuing.

Is this a ServerScript? If so, the event will not work. According to documentation, the service only works in LocalScripts.
(UserInputService | Roblox Creator Documentation)

1 Like

Oh yeah, that worked. I didn’t know that UIS could only be on local scripts. Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.