Hello devs!
I am making some sort of snowball fight game (4fun) and I need help with raycasting.
When the snowball is thrown by a player, sometimes the raycast doesn’t work.
If the snowball hits something, it just destroys itself.
The snowball relies on Roblox gravity and the direction it’s thrown.
I was trying to find the solution but nothing worked.
Example Script:
local Players = game:GetService("Players")
local Tool = script.Parent
local Animations = Tool.Animations
local Remotes = Tool.Remotes
local Config = Tool.Configuration
local Particles = Tool.Particles
local SnowballTemplate = Tool.Handle
local ThrowRE = Remotes.Throw
local ThrowAnim = Animations.Throw
local Debounce = false
local PlayerDebounces = {}
local RayParams = RaycastParams.new()
RayParams.IgnoreWater = true
RayParams.FilterType = Enum.RaycastFilterType.Exclude
ThrowRE.OnServerEvent:Connect(function(Plr, MousePos : CFrame)
SnowballTemplate.Transparency = 1
RayParams.FilterDescendantsInstances = {Plr.Character}
local NewSnowball = SnowballTemplate:Clone()
NewSnowball.AssemblyLinearVelocity = MousePos.LookVector * 200
NewSnowball.Transparency = 0
NewSnowball.Trail.Enabled = true
NewSnowball.Parent = workspace
local Position = NewSnowball.Position
local Velocity = MousePos.LookVector.Unit * 200
local LastPosition = NewSnowball.Position
local RayCast = coroutine.wrap(function()
while task.wait() do
local deltaTime = task.wait()
local NewPosition = Position + Velocity * deltaTime
local Result = workspace:Raycast(Position, NewPosition - Position, RayParams)
if Result and Result.Instance then
print(Result)
local Player = Players:GetPlayerFromCharacter(Result.Instance.Parent)
if Player then
if PlayerDebounces[Player] then
return
end
PlayerDebounces[Player] = true
local Character = Player.Character
local Humanoid = Character.Humanoid
Humanoid:TakeDamage(Config.Damage.Value)
task.wait(1)
PlayerDebounces[Player] = false
end
NewSnowball:Destroy()
break
end
Position = NewPosition
end
end)
RayCast()
task.wait(Config.Cooldown.Value)
SnowballTemplate.Transparency = 0
end)
The snowball can go through terrain and other models if it’s thrown far away.