Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempting to raycast bullet trajectories, what am i doing wrong?

Asked by
gunter5 17
4 years ago

This is my server side script. When I shoot the weapon of choice most of the bullets are laggy. if clicking rapidly, bullets fly at set velocity. My question is, am I able to fix this. Also in the firefunction() how would i code in a player holding the mouse, coding that client side and bringing a boolean over to verify if the player is holding the mouse or not simply doesn't work.

local itemstore = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("ItemDatabase"))
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("GunFiredEvent")
local TS = game:GetService("TweenService")
local goal = {}
local guntype = nil
local dmg = nil
local firerate = nil
local magazine = nil
local canfire = nil
local velocity = nil

RE.OnServerEvent:Connect(function(player, tool, weapon, firing, mousepos)
    for i,v in pairs(itemstore.Weapons) do
        print(tostring(i))
        if i == weapon then
            for x,y in pairs(v) do
                -- gather item statistics
                if x == "guntype" then
                    guntype = y
                    print("Weapon: "..guntype)
                end
                if x == "dmg" then
                    dmg = y
                    print("Damage: "..dmg)
                end
                if x == "firerate" then
                    firerate = y/60
                    print("Firerate (in rounds per second): "..firerate)
                end
                if x == "velocity" then
                    velocity = y
                    print("Velocity (in studs per second): "..velocity)
                end
            end
        end
    end
    firefunction(player, tool, weapon, firing, mousepos)
end)

function firefunction(player, tool, weapon, firing, mousepos)
    --if firing then -- [[Bullethole generation]]
    --  local ray = Ray()
    --  print(player.Name.." has fired weapon: "..weapon)
    --  local x = Instance.new("Part")
    --  x.Position = mouse
    --  x.Parent = workspace
    if firing then --add support for automatic weaponry
        firing = false
        local tip = tool:FindFirstChild("Tip")
        local origin = tip.Position
        local direction = (mousepos - origin).Unit
        local result = workspace:Raycast(origin, direction*100)

        local intersection = result and result.Position or origin + direction*300
        local distance = (origin - intersection).Magnitude

        local bullet = game:GetService("ServerStorage"):WaitForChild("Bullet"):Clone()
        bullet.CFrame = CFrame.new(origin, intersection)*CFrame.new(0,0,-distance)
        bullet.Position = tip.Position
        bullet.Size = Vector3.new(.05,.05,.5)
        bullet.Anchored = true
        bullet.Parent = workspace
        bullet.Touched:Connect(function(hit)
            if hit.Parent.Name ~= weapon and hit.Parent.Name ~= player and hit.Parent.Parent.Name ~= player then
                local hum = hit.Parent:FindFirstChild("Humanoid")
                if hum ~= nil and hum.Parent.Name ~= player then
                    hum:TakeDamage(dmg)
                end
            end
        end)
        if mousepos ~= nil then
            goal.Position = mousepos
        else
            goal.Position = origin + direction*300
        end
        local bullettime = distance/velocity
        local tinfo = TweenInfo.new(bullettime)
        local tween = TS:Create(bullet, tinfo, goal)
        tween:Play()
        game:GetService("Debris"):AddItem(bullet, bullettime)
        firing = true
    end
end
0
Oh, and I am also having a hit detection issue. if anyone knows a better way to do it please feel free to fix it. gunter5 17 — 4y
0
I fixed the hit detection issue by just not using the .Touched event as it is unreliable. Instead I now deal damage with the raycast itself and simply project the bullet for visuals. gunter5 17 — 4y

Answer this question