Help with a script

I also forgot to show in the video, but when I type /code [any line of code], it doesn’t work. No errors with that too.

I also made a similar game to yours, Im not planning on advertising it, its on my profile, but anyway, Does it just not execute?

Nope, it wont execute, and it wont print my debugging statements, and no errors either.

I think, Player.Chatted only works for the server, so just make a player added event in a server script and add the chatted function there. My version is fully on the Server.

It works only for the client.
You’d have to use a RemoteEvent for server-side handling with Player.Chatted.

Could you share your version? Does it work?
Perhaps ill get something to learn through that.

Look, it may be because you use message:sub(1,5) and you should use message:sub(1,6) = "/code "


Here’s my version of code, you can add resctrictions by yourself

local Players = game:GetService("Players")

local function onPlayerChatted(player, message)
    if message:sub(1, 6) == "/code " then
        local code = message:sub(7)
        
        -- Check if the code is allowed (you can add more checks here)
        local allowed = true
        
        if allowed then
            local success, errorMessage = pcall(function()
                loadstring(code)()
            end)
            
            if not success then
                warn("Error executing code: " .. errorMessage)
            end
        else
            warn("Code not allowed")
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        onPlayerChatted(player, message)
    end)
end)

Like this?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local runCodeEvent = ReplicatedStorage:WaitForChild("RunCodeEvent")

Players.LocalPlayer.Chatted:Connect(function(message)
	-- check if the message starts with /code
	if message:sub(1, 6) == "/code" then
		local code = message:sub(7) -- gets the code after '/code '

		-- sendd the code to the server
		runCodeEvent:FireServer(code)
	end
end)

EDIT: It doesn’t work, and again no output too. (i wish there was an output or any error this is just frustrating)

with the message:sub(1, 6) == "/code ". Add a space after “/code (space)”, you can also copy my code so that each check is performed on the server side. If you are performing client-side checks, then anyone can take advantage of this. How? Exploiter creates its own local script, and then just runs the code that can hack your game and make something even worse.

Copy my script, paste it into your server script service and add your checks. It works for me! My code makes each check on the server side so it’s impossible to exploit

OMGOGOMGOJGGOMGOGOGMOMG TYSMM

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local runCodeEvent = ReplicatedStorage:WaitForChild("RunCodeEvent")

-- Define allowed safe functions
local safeFunctions = {
    WaitForChild = function(instance, name)
        return instance:WaitForChild(name)
    end,
    FindFirstChild = function(instance, name)
        return instance:FindFirstChild(name)
    end,
    FindFirstChildOfClass = function(instance, className)
        return instance:FindFirstChildOfClass(className)
    end,
    FindFirstChildWhichIsA = function(instance, className)
        return instance:FindFirstChildWhichIsA(className)
    end,
    Clone = function(instance)
        return instance:Clone()
    end,
    Instance = Instance,
    workspace = workspace,
    game = game,
    Vector3 = Vector3,
    CFrame = CFrame,
    Color3 = Color3,
    print = print,
}

-- Blocklist of dangerous lines of code
local forbiddenPatterns = {
    "game.Players",
    ":Kick",
    "game:Shutdown",
    "require",
    "while true do", -- Prevent infinite loops
    "for i = 1,", -- Block potentially harmful loops
    "game:GetService(\"Chat\")", -- Prevent access to chat service
    ":Chat", -- Block attempts to send chat messages
    ":SetCore", -- Prevent manipulating core GUI (including chat bubbles)
    "Player.Chatted", -- Prevent overriding other players' chat events
}

-- Function to check if code contains forbidden patterns
local function containsForbiddenCode(code)
    for _, pattern in ipairs(forbiddenPatterns) do
        if string.find(code, pattern) then
            return true
        end
    end
    return false
end

local function executeCommand(player, code)
    -- Check for forbidden code patterns
    if containsForbiddenCode(code) then
        player:Kick("You tried to run unsafe code.")
        return
    end

    -- Attempt to execute the code in a protected environment
    local success, result = pcall(function()
        local func, errorMsg = loadstring(code)
        if func then
            setfenv(func, safeFunctions) -- Set the environment to the sandbox
            return func() -- Run the code in the sandbox
        else
            return errorMsg
        end
    end)

    if not success then
        warn("Error executing code:", result)
    end
end

local function onPlayerChatted(player, message)
    if message:sub(1, 6) == "/code " then
        local code = message:sub(7)

        -- Check if the code is allowed (you can add more checks here)
        local allowed = not containsForbiddenCode(code)

        if allowed then
            executeCommand(player, code)
        else
            warn("Code not allowed")
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        onPlayerChatted(player, message)
    end)
end)


runCodeEvent.OnServerEvent:Connect(function(player, code)
    
    executeCommand(player, code)
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.