I am attempting to make a spear tween to the mouse position, like this:
https://gyazo.com/15b9ddb5a42da407627aede959fe8546
But I am not sure how I am able to do this with Ray and TweenService with Mouse.
Here is what happens to me:
ServerScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local spearEvent = ReplicatedStorage:WaitForChild('SpearEvent')
local mouseMoveEvent = ReplicatedStorage:WaitForChild('MouseMoveEvent')
local iceSpear = ReplicatedStorage:WaitForChild('IceSpear')
local maxDistance = 50
local goal = {}
local tweenInfo = TweenInfo.new(0.25)
local damage = 50
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local spearFolder = Instance.new("Folder", character)
spearFolder.Name = 'SpearFolder'
end)
end)
local function repeatSpears(player, mouseP)
for i = 1, 4, 1 do
local character = player.Character
local cloned = iceSpear:Clone()
local based = cloned.Inv
cloned.Parent = player.Character.SpearFolder
cloned.Name = "IceSpear"..i
cloned:SetPrimaryPartCFrame(CFrame.new(character.Head.Position + Vector3.new(math.random(-2, 2), math.random(1, 4), math.random(-2, 2)), mouseP)
* CFrame.Angles(math.rad(0), math.rad(-90), 0))
end
wait(1)
end
local function spearCreate(player, mouseP, distance, origin, mouseHit)
repeatSpears(player, mouseP)
local spearRay = Ray.new(origin, (mouseP - origin).Unit * maxDistance)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(spearRay, {player.Character})
local distance = (origin - mouseP).magnitude
for i, v in pairs(player.Character.SpearFolder:GetChildren()) do
if distance < maxDistance and hit and hit.Parent:FindFirstChild('Humanoid') then
hit.Parent.Humanoid:TakeDamage(15)
end
goal.CFrame = v.Inv.CFrame * CFrame.new(-mouseHit.Position)
local tween1 = TweenService:Create(v.Inv, tweenInfo, goal)
tween1:Play()
tween1.Completed:Wait()
Debris:AddItem(v, 2.5)
end
end
spearEvent.OnServerEvent:Connect(function(player, origin, mousePos, mouseHit)
local spearRay = Ray.new(origin, (mousePos - origin).Unit * maxDistance)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(spearRay, {player.Character})
local distance = (origin - mousePos).magnitude
if distance < maxDistance and hit then
spearCreate(player, mousePos, distance, origin, mouseHit)
end
end)
LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spearEvent = ReplicatedStorage:WaitForChild('SpearEvent')
local mouseMoveEvent = ReplicatedStorage:WaitForChild('MouseMoveEvent')
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local mouse = player:GetMouse()
local used = false
local cooldown = 2.5 -- time between each shot
tool.Activated:Connect(function()
if not used then
used = true
local origin = character:WaitForChild('Head').Position
spearEvent:FireServer(origin, mouse.Hit.Position, mouse.Hit)
wait(cooldown)
used = false
end
end)