Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

The player can say a spell and keep using the same key to fire the event, how may i stop this?

Asked by 2 days ago

Hi! Thank you for taking the time to help me.

So my issue today is that I want to create a spell, so when a player says a certain string of words the right key is pressed, and if the mouse position is on another player then it should fire a remote event to push a player or other spell types appropriate to my game. My main concern here is that when the spell has been cast, everything runs smoothly except the player doesn't need to say the spell again, the player can continuously press the key and keep casting the spell. My question is, is there a way to deactivate the function to somehow to unbind the key so that the player needs to repeat the spell. I know about UserContextActionService and that is a great alternative but that is simply not what I need so can anyone please help with this issue. I have attached a simple script I made as a means for a testing ground and the issue remains to be the same which is that the player can press the key and the event will happen without having to repeat the spell in chat. The current script is a local one, This script is simple but it has the same problem, please contact me and thank you!

local player = game.Players.LocalPlayer local char = player.Character local UIS = game:GetService("UserInputService")

player.Chatted:Connect(function(msg) if string.lower(msg) == "Spell" then UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.Q then local part = Instance.new("Part") part.Parent = workspace end

    end)
end

end)

1 answer

Log in to vote
0
Answered by 2 days ago
Edited 2 days ago

you should add something to disconnect the key input function like:

local player = game.Players.LocalPlayer local char = player.Character local UIS = game:GetService("UserInputService")

local connection

player.Chatted:Connect(function(msg) if string.lower(msg) == "Spell" then 
connection = UIS.InputBegan:Connect(function(input) 
if input.KeyCode == Enum.KeyCode.Q then 
local part = Instance.new("Part") 
part.Parent = workspace 
connection:Disconnect()
end
end)
end

maybe u also can add disconnect it after a phew seconds if player don't press Q

local player = game.Players.LocalPlayer local char = player.Character local UIS = game:GetService("UserInputService")
local disconnecttime = 5
local connection

player.Chatted:Connect(function(msg) if string.lower(msg) == "Spell" then 
connection = UIS.InputBegan:Connect(function(input) 
if input.KeyCode == Enum.KeyCode.Q then 
local part = Instance.new("Part") 
part.Parent = workspace 
connection:Disconnect()
end
wait(disconnecttime)
if connection then
connection:Disconnect()
end
end)
end
0
Wow thank you so much! I was not aware of 'Connect', I will definetly try this out! Thank you! EliteForce1420 0 — 2d
Ad

Answer this question