How to use a variable nested in a function in another function?

I have a variable that I need to nest in an OnServerEvent function as it uses the player to get data. I also need to use this variable in another function that connects whenever my ray hits my target. This is done through fast cast. Unfortunately, I am unsure how to go about doing this.

--these two variables are nested in an OnServerEvent function
local function knifeThrow(player)
	local knife = Knives[profile.Data.knifeSave]
	local knifeCache = PartCache.new(knife, 50, knifeContainer)
end

--need knifecache for this function
local function knifeHit(cast, result, velocity, knife)
	delay(2, function()
		knifeCache:ReturnPart(knife)
	end)
end

--functions are connected here
caster.RayHit:Connect(knifeHit)
Remotes.KnifeThrow.OnServerEvent:Connect(knifeThrow)

If anyone could recommend that best way to attack this problem that would be amazing, thanks!

2 Likes

Is this all in the same script? If so you can always global the variable to the whole script.

local knifeCache
function knifeThrow(player)
	local knife = Knives[profile.Data.knifeSave]
	knifeCache = PartCache.new(knife, 50, knifeContainer)
end

--need knifecache for this function
function knifeHit(cast, result, velocity, knife)
	delay(2, function()
		knifeCache:ReturnPart(knife)
	end)
end

--functions are connected here
caster.RayHit:Connect(knifeHit)
Remotes.KnifeThrow.OnServerEvent:Connect(knifeThrow)

Like this …
knifeCache is global to the whole program.
knife is only “global” to the function knifeThrow().

If you’re towing many knifes this may be a better way of doing that …

local function createKnifeFunctions()
    local knifeCache

    local function knifeThrow(player)
        local knife = Knives[profile.Data.knifeSave]
        knifeCache = PartCache.new(knife, 50, knifeContainer)
    end

    local function knifeHit(cast, result, velocity, knife)
        delay(2, function()
            knifeCache:ReturnPart(knife)
        end)
    end

    return knifeThrow, knifeHit
end

local knifeThrow, knifeHit = createKnifeFunctions()

caster.RayHit:Connect(knifeHit)
Remotes.KnifeThrow.OnServerEvent:Connect(knifeThrow)
2 Likes

haha I didn’t try that because I thought that I wouldn’t be defining knifeCache until the remote was fired, but I wasn’t thinking because my RayHit function isnt going to fire until my remote is done. Thank you!

This more like top down programming. The 2nd version prevents global scope pollution and encapsulates the variables within a local scope.

1 Like

would there be a way to define knife at the top and define knifeCache outside the function? your solution worked but brought about another error (classic). the problem is that PartCache can’t be called while it’s in the middle of the delay. While it’s in the remote event it’s going to be called over and over, so it needs to go outside of it. I tried to make a global knife variable at the top but because knife isn’t defined yet I get an error when defining knifeCache

1 Like

Sometimes it needs to be something … did you try: local knife = nil
As the global define.

1 Like

probably a bad explanation on my end but what I meant was, knife isn’t defined until my remote is fired. My remote isn’t fired until I click. Once the game starts this is going to run if I put it outside the function

local knifeCache = PartCache.new(knife, 50, knifeContainer)

knife is yet to be defined when the game starts so I get thrown an error. I could probably constantly check what knife and whenever it’s finally defined run the function but I’m not sure that would be efficient.

1 Like

Try …
local knifeCache, knife = nil, nil
for your global defines up top.

local function createKnifeFunctions()
    local knifeCache, knife = nil, nil

    local function knifeThrow(player)
        knife = Knives[profile.Data.knifeSave]
        knifeCache = PartCache.new(knife, 50, knifeContainer)
    end

    local function knifeHit(cast, result, velocity)
        delay(2, function()
            knifeCache:ReturnPart(knife)
        end)
    end

    return knifeThrow, knifeHit
end

local knifeThrow, knifeHit = createKnifeFunctions()

caster.RayHit:Connect(knifeHit)
Remotes.KnifeThrow.OnServerEvent:Connect(knifeThrow)
2 Likes

doesn’t work, ill either use run service to always look for that variable or ill try to use a remote function. Thanks for the help though!

1 Like

Dang … there is always making a variable to work with.

local Knife = script.Parent.Knife
Knife.Value = (set this)

Then you can use Knife.Value from anyplace.
Kind of a hand made global …

the value I index knife with “profile.Data.knifeSave” is a string that saves whatever knife the player ownes. “Knife” is a folder in replicated storage with a bunch of tools in it

Well that is just a name … try a different one. KnifeToss.
Hate guessing here … Just don’t know how you got this set up in full.

Yeah I understand, there’s too many scripts and things going on to post everything here. I’ll have to experiment until I get things to work out

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