I'm making a Roblox code executor that is server sided but everytime I put in code like "print('Hello World!')" it runs that code two times for some reason.
Here is the Client Code:
local textbox = script.Parent
local Attempts = 0
local event = game.ReplicatedStorage.FireCode
local Debounce = false
textbox.FocusLost:Connect(function(enterPressed)
if enterPressed == true and Debounce ~= true then
Debounce = true
repeat
Attempts += 1
print(Attempts)
event:FireServer(textbox.Text)
task.wait()
until Attempts == 1
task.wait(1)
Attempts = 0
else
return
end
Debounce = false
end)
Here is the Server Code:
local event = game.ReplicatedStorage.FireCode
local debounce = false
event.OnServerEvent:Connect(function(player, code)
if player and player ~= nil and debounce ~= true then
debounce = true
loadstring(tostring(code))()
task.wait(0.1)
debounce = false
end
end)
There are no errors with the code.
I tried using the debounce method but it didn't work.
Not sure why you are calling the event inside a loop, even if that loop should only execute once. Here's how I would approach a debounced event handler.
It might be worth searching your Workspace when the game is running to see if you somehow have clones of this LocalScript somewhere.
local textbox = script.Parent
local event = game.ReplicatedStorage.FireCode
local debounce = false
local COOLDOWN = 2.0 -- seconds
textbox.FocusLost:Connect(function(enterPressed)
-- escape if the flag is active
if debounce then
return
end
if enterPressed then
debounce = true
-- get the code and then clear the text box so we don't repeat the same command
local code = textbox.Text
textbox.Text = ""
-- execute the code
event:FireServer(code)
-- wait for a moment before clearing the debounce flag
wait(COOLDOWN)
debounce = false
end
end)