I have created a system that takes the user input to fire a ray; This ray is then fired across the server and then firesallclients to produce a projectile which tweens to the end of the ray - or to the max distance the ray is allowed to travel..
I have attempted to now cause damage to a player if the projectile hits the player upon collision.. But the RayCast is causing the damage to happen before the projectile even hits the player..
Any ideas?
Local:
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Spells = game.ReplicatedStorage.Spells
local WandClicks = game.ReplicatedStorage.WandClicks
local Debris = game:GetService("Debris")
local Tool = script.Parent
local plr = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
repeat wait() until plr:HasAppearanceLoaded()
raycastParams.FilterDescendantsInstances = plr.Character:GetDescendants()
local function getPosition(mouseXY)
local x,y = mouseXY.X,mouseXY.Y
local ray = Camera:ViewportPointToRay(x,y)
local result = workspace:Raycast(ray.Origin, ray.Direction*1000,raycastParams)
if result then
return result.Position
else
return ray.Origin+(ray.Direction*500)
end
end
Tool.Equipped:Connect(function(Mouse)
UIS.InputBegan:Connect(function(input,gp)
if not gp then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("send")
WandClicks:FireServer(getPosition(UIS:GetMouseLocation()), script.Parent)
end
end
end)
end)
Spells.OnClientEvent:Connect(function(Spell,SentFrom,SentTo)
local SpellCloned = Spell:Clone()
SpellCloned.CFrame = CFrame.new(SentFrom)
local tweenInfo = TweenInfo.new(2*(SentFrom-SentTo).Magnitude/100)
local goal = {
["CFrame"] = CFrame.new(SentTo)
}
local tween = TweenService:Create(SpellCloned, tweenInfo, goal)
SpellCloned.Parent = workspace
tween:Play()
tween.Completed:Connect(function(playbackState)
if playbackState == Enum.PlaybackState.Completed then
local HitEffect = game.ReplicatedStorage.SpellB
local HClone = HitEffect:Clone()
HClone.Position = SpellCloned.Position
SpellCloned:Destroy()
HClone.Parent = workspace
Debris:AddItem(HClone, 1)
end
end)
end)
Server:
--local x = game.ReplicatedStorage.Spell1:Clone()
--x.CFrame = CFrame.new(pos)
--x.Parent = workspace
local ReplicatedStorage = game.ReplicatedStorage
local Event = ReplicatedStorage.Spells
local Spell = ReplicatedStorage.Spell
ReplicatedStorage.WandClicks.OnServerEvent:Connect(function(player, mouseLocation, tool)
local rayOrigin = Vector3.new(tool.Handle.Position.X, tool.Handle.Position.Y+2, tool.Handle.Position.Z)
local SentFrom = rayOrigin
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = player.Character:GetDescendants()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayDirection = (mouseLocation-rayOrigin).Unit
local distance, result, maxdistance = 0, nil, 200
---
local count = 0
repeat
if distance < maxdistance then
result = workspace:Raycast(rayOrigin, rayDirection * 20, raycastParams)
rayOrigin += (rayDirection * 20)
distance += ((rayDirection * 20)).Magnitude
if result then
Event:FireAllClients(Spell,SentFrom,result.Position)
if result.Instance.Parent:findFirstChild("Humanoid") then
result.Instance.Parent:findFirstChild("Humanoid"):TakeDamage(50)
else
print("I hit something that wasn't a player - Whoops")
end
break
end
else
Event:FireAllClients(Spell,SentFrom,rayOrigin)
print("I hit nothing :( ")
break
end
count += 1
wait()
until count >= 20
---
end)
Thank you!

1 comment
Thank you for your answer; However, It has no use of RayCasting or FireAllClients() - It supports nothing of my question.