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!
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)
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!
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
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.
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)
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