Is there a way I can access a variable from a function in a seperate function?

I’m trying to access the kunaiClone variable in my throwKunai function so I can send the position of it to the server in the FireServer line, however I can’t seem to find a way to do so and I’ve been stuck for a while.

local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRP = char:WaitForChild("HumanoidRootPart")

local RS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local debounce = false
local CD = 1
local count = 0

local tpRemote = RS.Remotes.Teleport

local mouse = plr:GetMouse()

local KEY = Enum.KeyCode.V

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}

local camera = workspace.CurrentCamera

local kunai = RS.FX.RaijinKunai

local function throwKunai()
	
	local rayMaxDist = 1500
	local mousePos = UIS:GetMouseLocation()
	local rayOrigin = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local raycastResult = workspace:Raycast(rayOrigin.Origin, rayOrigin.Direction * rayMaxDist)
	

	if raycastResult == nil then print("nil")
		return
	end
	
	local kunaiPosition = humRP.Position
	
	local kunaiClone = kunai:Clone()
	kunaiClone.Parent = workspace.Map.Ignore
	kunaiClone.CanCollide = false
	kunaiClone.Anchored = true
	kunaiClone.CFrame = CFrame.lookAt(kunaiPosition, raycastResult.Position)  * CFrame.Angles(0, math.rad(-90), math.rad(90))
	-- Look at creates a CFrame that starts at a first position and points towards a second
	-- To flip the direction, multiply the CFrame.lookAt result by CFrame.Angles(0, math.rad(180), 0)

	local distance = (raycastResult.Position - kunaiPosition).Magnitude -- (Target Position - Object Position)
	local speed = 150
	
	local tweenTime = distance / speed
	
	local objHit = raycastResult.Instance
	
	local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local kunaiDirection =  {Position = raycastResult.Position}
	local tweenKunai = TS:Create(kunaiClone, tweenInfo, kunaiDirection)

	tweenKunai:Play()
	
	print(objHit)
	print("Pressed V")
	
end


UIS.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if inp.KeyCode == KEY and not debounce or count >= 1 then
		debounce = true
		count += 1
		if count <= 1 then
			throwKunai()
		end
		if count >= 2 then
			tpRemote:FireServer(kunaiClone.Position, kunai)
			count = 0
			print("reset count")
		end
		print(count)
		task.wait(CD)
		debounce = false
	end
end)


just return the kunaiClone and define it within InputBegan

function throwKunai()
	...
	return kunaiClone
end

...
local kunaiClone
if count <= 1 then
	kunaiClone = throwKunai()
end
if count >= 2 then
	tpRemote:FireServer(kunaiClone.Position, kunai)
    ...
1 Like

just tried this and it didn’t work

why not? did it throw any errors?

Just define kunai clone in the beginning and when you actually clone the kunai set it to that variable.

--Example use
local KunaiClone = nil

---Other code

if KunaiClone then
 --Send info to server from here
end
1 Like

no errors but it just teleports me to where the kunai is in replicated storage instead of the actual cloned kunai, maybe I did it wrong?

heres my code

local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRP = char:WaitForChild("HumanoidRootPart")

local RS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local debounce = false
local CD = 1
local count = 0

local tpRemote = RS.Remotes.Teleport

local mouse = plr:GetMouse()

local KEY = Enum.KeyCode.V

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}

local camera = workspace.CurrentCamera

local kunai = RS.FX.RaijinKunai

local function throwKunai()
	
	local rayMaxDist = 1500
	local mousePos = UIS:GetMouseLocation()
	local rayOrigin = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local raycastResult = workspace:Raycast(rayOrigin.Origin, rayOrigin.Direction * rayMaxDist)
	

	if raycastResult == nil then print("nil")
		return
	end
	
	local kunaiPosition = humRP.Position
	
	local kunaiClone = kunai:Clone()
	kunaiClone.Parent = workspace.Map.Ignore
	kunaiClone.CanCollide = false
	kunaiClone.Anchored = true
	kunaiClone.CFrame = CFrame.lookAt(kunaiPosition, raycastResult.Position)  * CFrame.Angles(0, math.rad(-90), math.rad(90))
	-- Look at creates a CFrame that starts at a first position and points towards a second
	-- To flip the direction, multiply the CFrame.lookAt result by CFrame.Angles(0, math.rad(180), 0)

	local distance = (raycastResult.Position - kunaiPosition).Magnitude -- (Target Position - Object Position)
	local speed = 150
	
	local tweenTime = distance / speed
	
	local objHit = raycastResult.Instance
	
	local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local kunaiDirection =  {Position = raycastResult.Position}
	local tweenKunai = TS:Create(kunaiClone, tweenInfo, kunaiDirection)

	tweenKunai:Play()
	
	print(objHit)
	print("Pressed V")
	
	return kunaiClone
end



UIS.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if inp.KeyCode == KEY and not debounce or count >= 1 then
		debounce = true
		count += 1
		if count <= 1 then
			throwKunai()
		end
		if count >= 2 then
			local kunaiClone = throwKunai()
			tpRemote:FireServer(kunaiClone.Position, kunai)
			count = 0
			print("reset count")
		end
		print(count)
		task.wait(CD)
		debounce = false
	end
end)


ill try this right now, thanks

theres the problem, youre calling the function again for some reason instead of doing it like i said above

if count <= 1 then
	throwKunai()
end
if count >= 2 then
	local kunaiClone = throwKunai()
	tpRemote:FireServer(kunaiClone.Position, kunai)
	count = 0
	print("reset count")
end

should be

local kunaiClone
if count <= 1 then
	kunaiClone = throwKunai()
end
if count >= 2 then
	tpRemote:FireServer(kunaiClone.Position, kunai)
	count = 0
	print("reset count")
end

though i dont understand why it is teleporting you, as how you did it would cause the kunai to be thrown 2 times, its probably the server sided script that receives the event that teleports you

I wanted it to do the teleport lol it was the server script, also ill try this aswell but the other one worked for me :+1: tysm

It’s good to hear your code now works and you’re making a naruto game? That’s very interesting. Are you making a flying thunder god kunai?

1 Like

I’m not making a naruto game, but I’m trying to learn scripting so I’m just making random skills and decided on making the flying thunder god kunai because I thought it would be simple, but it turns out its not so simple for me. :sob:

1 Like

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