I’m trying to make the cooldown tween start when i press Z, the event fires from the client but for some strange reason it just wont get past the if statment that check if the keycode is Z
I’ve tried anything logical for me by putting prints to know where it stops but i still cant achieve it…
tool.Equipped:Connect(function()
gui.Whole.Visible = true
script.Parent.Input.OnServerEvent:Connect(function(plr,inp)
debounce = true
if inp then
if inp.Keycode == Enum.KeyCode.Z then
print("Z move!")
task.spawn(PlayZtween)
end
end
task.wait(0.5)
debounce = false
end)
end)
You create a new connection for your RemoteEvent each time you equip your tool. This means that your code can be executed several times at once without this being intended. I advise you to put the Remote connection outside of tool.Equipped or disconnect this one when the tool is unequipped.
I also advise you to check the KeyCode on the client side in the UserInputService function rather than sending the InputObject to the server.
Like J_Angry say, it’s not Keycode but KeyCode with uppercase C.
These are the remarks I have to make on your code. This is what your code might look like once fixed:
Client:
local UserInputService = game:GetService("UserInputService")
local tool = script.Parent
local remote = tool:WaitForChild("Input")
local debounce = false
local equipped = false
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
if (inputObject.KeyCode == Enum.KeyCode.Z) and equipped and (not debounce) then
debounce = true
remote:FireServer()
task.wait(0.5)
debounce = false
end
end)
Server:
local tool = script.Parent
local remote = tool:WaitForChild("Input")
local equipped = false
tool.Unequipped:Connect(function()
equipped = false
gui.Whole.Visible = false
end)
tool.Equipped:Connect(function()
equipped = true
gui.Whole.Visible = true
end)
remote.OnServerEvent:Connect(function(player)
if equipped then
PlayZtween() -- task.spawn is useless here
end
end)
Let me know if this helped you. You can also ask me any questions if you have any.