Help with a script

Hi fellow scripters, I wanted some help with my script as it’s not working; but also not printing out any errors too even with debug print statements added.
This code is for a small little project I’m making, whenever the player types in /code before anything, and then enters a few lines of code; that code will be executed on the server.
Script in ServerScriptService:

loadstring("warn('Hello!')")()

local runCodeEvent = game:GetService("ReplicatedStorage").RunCodeEvent
local safeEnvironment = {
	print = print, 
	math = math,
	string = string,
	table = table,
	Instance = Instance,
	workspace = workspace,
	game = game, -- things like game:GetService i restricted some tho 
	Vector3 = Vector3,
	CFrame = CFrame,
	Color3 = Color3,


	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,
	Destroy = function(instance)
		return instance:Destroy()
	end,


	InstanceNew = function(className)
		return Instance.new(className)
	end
}

-- so that players cant abuse some functions 
local forbiddenPatterns = {
	"game.Players",
	":Kick",
	"game:Shutdown",
	"require",
	"while true do", -- this prevents infinite loops 
	"for i = 1,", -- blocks potentially harmful loops 
	"game:GetService(\"Chat\")",
	":Chat",
	":SetCore",
	"Player.Chatted",
	":Kick(",
	"Player:Kick(",
}


local function containsForbiddenCode(code)
	for _, pattern in ipairs(forbiddenPatterns) do
		if string.find(code, pattern) then
			return true
		end
	end
	return false
end

-- handles the code execution 
runCodeEvent.OnServerEvent:Connect(function(player, code)
	-- safety check 
	if containsForbiddenCode(code) then
		player:Kick("You tried to run unsafe code.")
		return
	end

	-- tries to run the code 
	local success, result = pcall(function()
		-- compile the code into a function
		local func = loadstring(code) -- this is where it tries to run the code, but cant and doesnt give any errors either 
		if func then

			setfenv(func, safeEnvironment) 
			return func()  
		else
			return "Invalid code."
		end
	end)


	if success then
		print("Code ran successfully:", result)
	else
		print("Error executing code:", result)
	end
end)

LocalScript in StarterPlayerScripts:

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, 5) == "/code" then
		local code = message:sub(7) -- gets the code after '/code '

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

its turned off for security purposes as its very powerful and vulnerable, i dont think you can turn it on anymore and roblox will probably be deprecating it soon.

1 Like

So you mean to say that even if I tick the checkbox in studio, it still wont enable in play tests or servers?

Working fine on my end. Make sure you’re using it on the Server and not the Client.

The entire point of the checkbox is to allow Scripts to use it. Removing it wouldn’t be backwards-compatible :upside_down_face: Please be careful with your words.

It turns off as soon as I play test the game though.

1 Like

Does it give an error message? How are you checking?

No error messages. Do you want the script?

1 Like

If there is no error message, then it should be running.

Are you looking at the checkbox through Properties? Then you should know, LoadStringEnabled is not replicated:

, so, on the Client, it will just appear as it’s default value (false) when looking at it through Properties.

Oh, but shouldnt it replicate in my context? I’m using a RemoteEvent.
This script is a small little project im working on, this is a Script in ServerScriptService which is triggered from a LocalScript when the RemoteEvent is called.

local safeEnvironment = {
    print = print, 
    math = math,
    string = string,
    table = table,
    Instance = Instance,
    workspace = workspace,
    game = game, -- things like game:GetService i restricted some tho 
    Vector3 = Vector3,
    CFrame = CFrame,
    Color3 = Color3,

   
    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,
    Destroy = function(instance)
        return instance:Destroy()
    end,

     
    InstanceNew = function(className)
        return Instance.new(className)
    end
}

-- so that players cant abuse some functions 
local forbiddenPatterns = {
    "game.Players",
    ":Kick",
    "game:Shutdown",
    "require",
    "while true do", -- this prevents infinite loops 
    "for i = 1,", -- blocks potentially harmful loops 
    "game:GetService(\"Chat\")",
    ":Chat",
    ":SetCore",
    "Player.Chatted",
    ":Kick(",
    "Player:Kick(",
}

 
local function containsForbiddenCode(code)
    for _, pattern in ipairs(forbiddenPatterns) do
        if string.find(code, pattern) then
            return true
        end
    end
    return false
end

-- handles the code execution 
runCodeEvent.OnServerEvent:Connect(function(player, code)
    -- safety check 
    if containsForbiddenCode(code) then
        player:Kick("You tried to run unsafe code.")
        return
    end

    -- tries to run the code 
    local success, result = pcall(function()
        -- compile the code into a function
        local func = loadstring(code) -- this is where it tries to run the code, but cant and doesnt give any errors either 
        if func then
             
            setfenv(func, safeEnvironment) 
            return func()  
        else
            return "Invalid code."
        end
    end)

     
    if success then
        print("Code ran successfully:", result)
    else
        print("Error executing code:", result)
    end
end)

and the LocalScript (in StarterPlayerScripts)

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, 5) == "/code" then
        local code = message:sub(7) -- Get the code after '/code '
        
        -- sendd the code to the server
        runCodeEvent:FireServer(code)
    end
end)

So, what’s the result? If you just put loadstring("warn('Hello!')")() at the top of the Script does it print a warning to the Output?

You mean to say replace loadstring(code) with (loadstring("warn('Hello!')")?
If I did that, the code would be messed up and would error.

To check that loadstring itself is not the issue put loadstring("warn('Hello!')")() at the very top of the Server script and see if it prints.

The LoadStringEnabled property in ServerScriptService is designed to control whether the loadstring function can be used by server scripts. By default, this property is set to false for security reasons, as enabling it can lead to remote code execution vulnerabilities Here’s a link for the documentation.

When you try to enable LoadStringEnabled in Roblox Studio and it turns off during play testing, it indicates that the property is not meant to be changed dynamically or might be restricted by Roblox’s security protocols. This behavior is consistent both in Studio and on live servers. If you need to use dynamic code execution, consider alternative approaches that do not rely on loadstring , as keeping this property disabled is generally recommended for maintaining the security of your game.

seems like im late sorry for the misinformation

It does warn “Hello!”.

3030303030characterlimit

Seems like I need help with my script then. Ill edit this topic while you can have a look at my script.

loadstring("warn('Hello!')")()

local runCodeEvent = game:GetService("ReplicatedStorage").RunCodeEvent
local safeEnvironment = {
	print = print, 
	math = math,
	string = string,
	table = table,
	Instance = Instance,
	workspace = workspace,
	game = game, -- things like game:GetService i restricted some tho 
	Vector3 = Vector3,
	CFrame = CFrame,
	Color3 = Color3,


	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,
	Destroy = function(instance)
		return instance:Destroy()
	end,


	InstanceNew = function(className)
		return Instance.new(className)
	end
}

-- so that players cant abuse some functions 
local forbiddenPatterns = {
	"game.Players",
	":Kick",
	"game:Shutdown",
	"require",
	"while true do", -- this prevents infinite loops 
	"for i = 1,", -- blocks potentially harmful loops 
	"game:GetService(\"Chat\")",
	":Chat",
	":SetCore",
	"Player.Chatted",
	":Kick(",
	"Player:Kick(",
}


local function containsForbiddenCode(code)
	for _, pattern in ipairs(forbiddenPatterns) do
		if string.find(code, pattern) then
			return true
		end
	end
	return false
end

-- handles the code execution 
runCodeEvent.OnServerEvent:Connect(function(player, code)
	-- safety check 
	if containsForbiddenCode(code) then
		player:Kick("You tried to run unsafe code.")
		return
	end

	-- tries to run the code 
	local success, result = pcall(function()
		-- compile the code into a function
		local func = loadstring(code) -- this is where it tries to run the code, but cant and doesnt give any errors either 
		if func then

			setfenv(func, safeEnvironment) 
			return func()  
		else
			return "Invalid code."
		end
	end)


	if success then
		print("Code ran successfully:", result)
	else
		print("Error executing code:", result)
	end
end)

LocalScript:

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, 5) == "/code" then
		local code = message:sub(7) -- gets the code after '/code '

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

Welp, i did try to fix your code, i can’t check it so i’ll wait for a feedback!


ServerScript

local runCodeEvent = game:GetService("ReplicatedStorage").RunCodeEvent

local safeEnvironment = {
    print = print,
    math = math,
    string = string,
    table = table,
    Instance = Instance,
    workspace = workspace,
    game = game,
    Vector3 = Vector3,
    CFrame = CFrame,
    Color3 = Color3,
    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,
    Destroy = function(instance)
        return instance:Destroy()
    end,
    InstanceNew = function(className)
        return Instance.new(className)
    end
}

local forbiddenPatterns = {
    "game.Players",
    ":Kick",
    "game:Shutdown",
    "require",
    "while true do",
    "for i = 1,",
    "game:GetService(\"Chat\")",
    ":Chat",
    ":SetCore",
    "Player.Chatted",
    ":Kick(",
    "Player:Kick("
}

local function containsForbiddenCode(code)
    for _, pattern in ipairs(forbiddenPatterns) do
        if string.find(code, pattern) then
            return true
        end
    end
    return false
end

runCodeEvent.OnServerEvent:Connect(function(player, code)
    if containsForbiddenCode(code) then
        player:Kick("You tried to run unsafe code.")
        return
    end

    local success, result = pcall(function()
        local func = loadstring(code)
        if func then
            setfenv(func, safeEnvironment)
            func()
        else
            error("Failed to load code.")
        end
    end)

    if not success then
        warn("Error running code: " .. tostring(result))
    end
end)

No, that doesn’t work. You simplified it I assume? It’s pretty short in terms of lines of code compared to mine.

yeah i did. Just doesn’t work? No errors or something else? No output? Try checking your local script (check if message:sub(1, 5) == "/code" by doing print of it’s value.) Any feedback can help me understand and help you more!

No errors, no print statements which I added for debugging; nothing.