How would I add a key press to this script?

I am trying to add a key press to a function. However, I need to add it to a function that detects when a event fires. I’m not sure how to add it without using a function, but you can’t wrap functions in eachother.
my code:

game.ReplicatedStorage.WaterTouched.OnClientEvent:Connect(function()
	local Plr = game.Players.LocalPlayer
	local ThirstVal = Plr:WaitForChild("ThirstVal") 
	print("RemoteEvent Activated") --checking if code worked/eventfired
if ThirstVal.Value < 100 then
	LoadedAnimation:Play()
	hum.WalkSpeed = 0
	task.wait(10)
	ThirstVal.Value = ThirstVal.Value + 20
	hum.WalkSpeed = 16
	LoadedAnimation:Stop()
if ThirstVal.Value > 100 then
		LoadedAnimation:Play()
		hum.WalkSpeed = 0
		task.wait(10)
		ThirstVal.Value = 100
		hum.WalkSpeed = 16
		LoadedAnimation:Stop()
		end
	end
end)
1 Like

You can use variables to see if the event is fired and/or the key is pressed and run the function accordingly.

2 Likes

How would I do that? Would it use a if statement?

You can wrap a function in another. Just disconnect it once you’re done

I’m trying to make it so if I press a button it activates the code, but it needs to check that the event has been fired.

Yes, it would. You first would create a variable that would see if the player is pressing a key. And then when the event would fire you would see if that key is being pressed or not and then accordingly run your function.

You can use a variable in the same method above but just setting it to true as the event get’s fired and then if the key gets pressed and the variable is true then you can run your function. Something like this:

local eventRecieved = false
Event.OnClientEvent:Connect(function()
	eventRecieved = true
end)

UserInputService.InputBegan:Connect(function(input,gameProcessedEvents)
	if input.KeyCode == Enum.KeyCode[Key] and eventRecieved then
		Run_My_Function()
	end
end)
3 Likes

Make a boolean to hold the status of the event to be fired and check for it when the key is pressed

Edit: vouch for @awry_y

1 Like

Works great, I added it to my code. Here’s the finished code:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://17075750640"
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local LoadedAnimation = animator:LoadAnimation(animation)

local UserInputService = game:GetService("UserInputService")
local eventRecieved = false

game.ReplicatedStorage.WaterTouched.OnClientEvent:Connect(function()
	eventRecieved = true
end)

UserInputService.InputBegan:Connect(function(input,gameProcessedEvents)
	if input.KeyCode == Enum.KeyCode.V and eventRecieved then
		local Plr = game.Players.LocalPlayer
		local ThirstVal = Plr:WaitForChild("ThirstVal") 
		print("RemoteEvent Activated") --checking if code worked/eventfired
			LoadedAnimation:Play()
			hum.WalkSpeed = 0
			hum.JumpPower = 0
			task.wait(10)
			ThirstVal.Value = 100
			hum.WalkSpeed = 16
			hum.JumpPower = 50
			LoadedAnimation:Stop()
		end
end)

Great! I am glad that I was able to assist you! Have a great day!~

1 Like

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