Currently im working on a weapon system, the thing is I need to find where the raycast hit, is there any good way to do this?
This is my code
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local ammoCount = script.Parent:WaitForChild("AmmoCount")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gunShot = script.Parent.Gunshot
local updateAmmo = ReplicatedStorage.UpdateAmmo
local hasMoney = false
ammoCount.Value = 8
remote.OnServerEvent:Connect(function(player, position)
local origin = shoot_part.Position
local direction = (position - origin).Unit*250
local result = Workspace:Raycast(origin, direction)
if ammoCount.Value < 1 then
ammoCount.Value = 8
wait(2)
else
ammoCount.Value -= 1
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = ReplicatedStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
-- debugging shots
bullet_clone.CanCollide = false
bullet_clone.Anchored = true
if result then
local part = result.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(15)
print("Humanoid taking damage")
if humanoid.Health < 1 then
print("Player is now dead")
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 10
player.leaderstats.Kills.Value = player.leaderstats.Kills.Value + 1
hasMoney = true
humanoid.Name = "otherHumanoid"
else
hasMoney = false
end
end
-- smoke on hit
if part then
print("Generate smoke now")
end
end
wait(0.25)
--bullet_clone:Destroy()
gunShot:Play()
wait(2)
gunShot:Stop()
end
updateAmmo:FireClient(tool.Parent, ammoCount.Value)
end)