Why is loadstring not working?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I would like to make an command that runs code on the server side.

  2. What is the issue? Although the server is receiving the code, it is not running the code.
    I tried firing “print(‘Hello’)” But doesn’t run. I know it is receiving the code. What is wrong here? There arent any errors being displayed…

  3. What solutions have you tried so far? I confirmed that loadstring was enabled in server script service.

SERVER SCRIPT

   script.Parent.Eval.OnServerEvent:Connect(function(plr, Text)
	
	local token = "my token goes here, I'm not giving this away lol"
	if script.Parent.Parent.Parent.TOKEN.Value == token then
		print(Text)
		loadstring(Text)
	else
		plr:Kick("\n \n Nice try. \n Code: Ungranted Access to Owner Commands")
	end
end)

All help is appreciated!!!
P.S. Please also tell me if this is a security issue.

1 Like

You need to enabled ‘LoadStringEnabled’ in ServerScriptService’ properties.
ServerScriptService.LoadStringEnabled (roblox.com)

But you realise this is a VERY exploitable tool to enable?
You should look into validating the code being sent to the server (on the server) before running it - make sure it doesn’t affect other players - less you really don’t care and - well whatever. :slight_smile:

1 Like

As I said, it is enabled. Thanks though!

Edit:
image

1 Like

Oh, my apologies - completely missed that.

Well old fashioned diagnosing to the rescue - PRINT! :smiley:

  • Is the Client actually sending the request?
  • Is the Eval RemoteEvent being called?
  • Is the .TOKEN.Value reference correct? - try creating a hard reference to it outside of your Event
  • What is populating .TOKEN.Value?
local TOKEN = game.ScriptService:FindFirstChild("TOKEN",true) -- corrrect this as neccesarry

script.Parent.Eval.OnServerEvent:Connect(function(plr, Text)
	print("Player",plr,"called Eval Event with value",Text)

	if (TOKEN == nil) then print("Couldn't find TOKEN",TOKEN) return end

	local token = "my token goes here, I'm not giving this away lol"
	if TOKEN.Value == token then
		print("Player",plr,"loadstring executed:",Text)
		loadstring(Text)
	else
		print("Player",plr,"Failed Validation - Kicking")
		plr:Kick("\n \n Nice try. \n Code: Ungranted Access to Owner Commands")
	end
end)
1 Like

Like I said before, everything is working except the loadstring. I checked it

Heres proof

if script.Parent.Parent.Parent.TOKEN.Value == token then
		print("Here is the code being ran: " .. Text)
		loadstring(Text)
		
	else
		plr:Kick("\n \n Nice try. \n Code: Ungranted Access to Owner Commands")
	end

All of the above

Well, it should be equal to the variable token. Otherwise, it would kick me!

It also would not print “Here is the code being ran”
Please read more closely!

Thanks!

1 Like

Update: Added loadstring("print('hii')") at the start of the script but STILL not working. I am going to try and restart my computer…

1 Like

loadstring returns a function of the script that is compiled. You need to call the function in order for it to be ran (loadstring(source)())

4 Likes

I meant - are you adding a value from the client so that you can validate the token somehow - otherwise your logic is literally a “if true == true” statement.

Anyway, you’ve sorted the problem, happy Devving!

1 Like

Well, I am doing this to make sure that the client didn’t just clone it. And also its verifying it from the server so if the client changes the value, it wont do anything.