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