Advertisement
LikeRampage

CHATGPT take message in chat and execute code Loadstring roblox studio

Aug 12th, 2023 (edited)
1,453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. -- Function to handle chat messages
  2. local function onChat(message, player)
  3.     -- Execute the code within the message using loadstring and pcall
  4.     local success, dynamicFunction = pcall(loadstring, message)
  5.  
  6.     -- Display the result or error message
  7.     if success then
  8.         local status, error = pcall(dynamicFunction)
  9.         if status then
  10.             print("Code executed successfully!")
  11.         else
  12.             print("Error executing code: " .. tostring(error))
  13.         end
  14.     else
  15.         print("Error loading code: " .. tostring(dynamicFunction))
  16.     end
  17. end
  18.  
  19. -- Connect the onChat function to the Chatted event of the Players service
  20. game.Players.PlayerAdded:Connect(function(player)
  21.     player.Chatted:Connect(function(message)
  22.         onChat(message, player)
  23.     end)
  24. end)
  25.  
  26. -- Function to execute the code received from chat using loadstring
  27. local function executeCodeFromChat(code)
  28.     local success, dynamicFunction = pcall(loadstring, code)
  29.  
  30.     -- Display the result or error message
  31.     if success then
  32.         local status, error = pcall(dynamicFunction)
  33.         if status then
  34.             print("Code executed successfully!")
  35.         else
  36.             print("Error executing code: " .. tostring(error))
  37.         end
  38.     else
  39.         print("Error loading code: " .. tostring(dynamicFunction))
  40.     end
  41. end
  42.  
  43. -- Example usage: execute code from chat
  44. local codeFromChat = "!print('Hello, World!')"
  45. executeCodeFromChat(codeFromChat)
  46.  
  47. [[I apologize for the confusion. It seems that the loadstring function may not be available due to security restrictions in certain contexts, such as server-side scripts in Roblox Studio.
  48.  
  49. If you encounter the error "loadstring() is not available," it means that the loadstring function is disabled in that context. This is a security measure to prevent potentially malicious code execution.
  50.  
  51. In order to execute dynamic code in a safe and controlled manner, you might consider alternative approaches, such as creating a set of predefined commands or functions that users can trigger with chat messages. This allows you to control and validate the user input more effectively.
  52.  
  53. Remember to prioritize security and carefully review any code that involves executing user-submitted content to prevent any potential vulnerabilities]]
Tags: loadstring
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement