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