Variables in loadstring not working

so recently i added a feature to my game that allows players to run code on specific moments so my friend wanted to make a black hole code so i did
it worked fine but with the loadstring ingame feature it dindt
then i tried with loadstring in studio and it also dindt work
i think this is because i use a variable
but how to make variables work in loadstring

here is the black hole script btw


workspace:WaitForChild("BlackHole"):Destroy()
local met = Instance.new("Part")
met.Name = "BlackHole"
met.Shape = "Ball"
met.CFrame = script.Parent.HumanoidRootPart.CFrame
met.Anchored = true
met.CanCollide = false
met.Color = Color3.new(0, 0, 0)
met.Material = "Neon"
met.Parent = workspace
met.Touched:Connect(function(tpart)
	if not tpart:IsDescendantOf(script.Parent) then
		tpart:Destroy()
		met.Size = met.Size + Vector3.new(tpart.Size.Magnitude,tpart.Size.Magnitude,tpart.Size.Magnitude)
	end
end)

??? what happened why did u remove it

2 Likes

im probably never gonna get this fixed

I think this could be related to how Lua handles chunks. Try to remove the local part of the variable definition.
Edit: Nevermind. The reason the code is not working is because :WaitForChild() yields forever. Try this:

local Blackhole = workspace:FindFirstChild("BlackHole")
if (Blackhole) then
    Blackhole:Destroy()
end
...

Thank you, since its late i will try this tomorrow thanks for helping

It’s because how you handle getfenv() and setfenv() , you’re not sending a metamethod for __index.

local environment = setmetatable({
        print = function(...)
            log(tostring(...),'print') -- custom func you can implement
        end,
        warn = function(...)
            log(tostring(...),'warn')
        end,
        error = function(...)
            log(tostring(...),'error')
        end,
    },{
        __index = function(t,k)
            return env[k]
        end,
    })

local func,err = loadstring(code) -- code here

if err then
    warn(err)
else
    setfenv(func,environment) -- sets the environment of the loadstring, which runs it
end

i just woke up so im going to try this

now the loadstrings completly dont run anymore
here is the code that runs the loadstrings (after i put in ur code)

local ValueFolder = script.Parent:WaitForChild("ScriptValues")
local character = script.Parent
local plr = game.Players:GetPlayerFromCharacter(character)
local glove = plr.Backpack:GetChildren()[1]


script.Equipped.Event:Connect(function()
	local code = ValueFolder.OnEquip.Value
	local environment = setmetatable({
		print = function(...)
			log(tostring(...),'print') -- custom func you can implement
		end,
		warn = function(...)
			log(tostring(...),'warn')
		end,
		error = function(...)
			log(tostring(...),'error')
		end,
	},{
		__index = function(t,k)
			return env[k]
		end,
	})

	local func,err = loadstring(code) -- code here

	if err then
		warn(err)
	else
		setfenv(func,environment) -- sets the environment of the loadstring, which runs it
	end
end)

script.Unequipped.Event:Connect(function()
	local code = ValueFolder.OnUnEquip.Value
	local environment = setmetatable({
		print = function(...)
			log(tostring(...),'print') -- custom func you can implement
		end,
		warn = function(...)
			log(tostring(...),'warn')
		end,
		error = function(...)
			log(tostring(...),'error')
		end,
	},{
		__index = function(t,k)
			return env[k]
		end,
	})

	local func,err = loadstring(code) -- code here

	if err then
		warn(err)
	else
		setfenv(func,environment) -- sets the environment of the loadstring, which runs it
	end
end)

script.AbilityButtonPressed.Event:Connect(function()
	local code = ValueFolder.OnAbility.Value
	local environment = setmetatable({
		print = function(...)
			log(tostring(...),'print') -- custom func you can implement
		end,
		warn = function(...)
			log(tostring(...),'warn')
		end,
		error = function(...)
			log(tostring(...),'error')
		end,
	},{
		__index = function(t,k)
			return env[k]
		end,
	})

	local func,err = loadstring(code) -- code here

	if err then
		warn(err)
	else
		setfenv(func,environment) -- sets the environment of the loadstring, which runs it
	end
end)

script.OnHit.Event:Connect(function()
	local code = ValueFolder.OnHit.Value
	local environment = setmetatable({
		print = function(...)
			log(tostring(...),'print') -- custom func you can implement
		end,
		warn = function(...)
			log(tostring(...),'warn')
		end,
		error = function(...)
			log(tostring(...),'error')
		end,
	},{
		__index = function(t,k)
			return env[k]
		end,
	})

	local func,err = loadstring(code) -- code here

	if err then
		warn(err)
	else
		setfenv(func,environment) -- sets the environment of the loadstring, which runs it
	end
end)

You don’t have to paste the environment for every function, you can have that on the global scope and just re-use that every time you set the loadstring’s environment. I’ve noticed you also kept the log functions in, those do not exist and they exist within my code so you’d have to re-implement that with something else (or you could just remove it).

oh well i just removed ur code entirely then tried it with the other guys solution and it worked turns out there wasnt a problem after all but thank you for helping anyway!

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