Invalid argument #1 to 'loadstring' (string expected, got Instance)

  1. What do you want to achieve? Keep it simple and clear!

Good Morning / Afternoon / Night Fellow Pirates of Robloxian Forum!

Im making calculator with the usage of loadstring, though I really don’t know how to use loadstring feature that’s why i’m here (Sorry for the short explaining short time)

  1. What is the issue? Include screenshots / videos if possible!

Here is some of the script regarding the Issue.


-- Client Code

local CalculusParams = game.ReplicatedStorage:WaitForChild("GETCALC")
local Buttons = script.Parent.Buttons
local Result = Buttons.Equal
local Signals = Buttons.Signals
local Numbers = Buttons.Numbers
local TypeIn = script.Parent.TypeIn

local function PressResult()
	local Pattern = string.match(TypeIn.Text, "[%d+][%p][%d+]")
	print(Pattern)
	if Pattern then
		CalculusParams:FireServer(TypeIn.Text)
	end
end

Result.MouseButton1Down:Connect(PressResult)

-- Server Code

local CalculusParams = game.ReplicatedStorage:WaitForChild("GETCALC")

local function Math(Text)
	local f = loadstring(Text)
	print(loadstring(Text)())
	return f
end

CalculusParams.OnServerEvent:Connect(Math)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Looking trough old Answers of this exact problem, looking trough old Developer Forum website answers, other websites, lua coding itself, Asking someone and Developer Hub.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

In advance here are some cookies!! :cookie: :cookie:

The first argument of RemoteEvent.OnServerEvent listeners is always the player that fired the remote. You are trying to return something back to the client though, in that case use a RemoteFunction.

local function Math(_, Text)
	local f = loadstring(Text)
	
	if f then
		setfenv(f, { })
		return f()
	end

	return nil
end

CalculusParams.OnServerInvoke = Math

I also absolutely suggest you sandbox the code, since an exploiter could just execute arbitrary code.

2 Likes

Tysm!! Also THAT IS THE LANGUAGE OF THE GODS :flushed: