I know that spawning projectiles on server is a pretty bad practice so I strongly need and advice on how to move it to module or client script! I understand more or less which part I should put where, but how no idea how to connect them! How should I separate rendering projectile and hit detection and *GET RID OF THIS LAG??? Been trying to figure it out for a couple of weeks so help will be MUCH appreciated. So far projectile stutters sometimes or behaves as tho having some crazy velocity.
The video of the lag
Server Script
---Server script
local ts = game:GetService('TweenService')
local Debris = game:GetService("Debris")
local rep = game:GetService('ReplicatedStorage')
local rs = game:GetService("RunService").Heartbeat;
t = 0
script.Parent.WeaponFired.OnServerEvent:Connect(function(player, mousepos)
local star = rep.StarHitbox
local ch = player.Character
local hum = ch.Humanoid
local hrp = ch:WaitForChild('HumanoidRootPart')
local righthand = ch:WaitForChild('RightHand')
local tool = ch:FindFirstChild('Star')
local starclone = star:Clone()
starclone.CanCollide = true
starclone.Parent = workspace
starclone:SetNetworkOwner(player)
starclone.Orientation = Vector3.new(0, math.rad(-90), 0)
local distance = (mousepos - hrp.Position).magnitude
local x = print(distance)
local function set_t(distance)
if distance <= 300 then
t = 0.3
elseif distance > 300 and distance < 1000 then
t = 0.5
end
return t
end
t = set_t(distance)
--- here velocity and cframes are calculated based on t
Debris:AddItem(starclone, t+0.5)
local ontouch = starclone.Touched:Connect(function(hit)
local enemy = hit.Parent:FindFirstChild('Humanoid')
if enemy == hum then return end
if enemy and enemy ~= hum then
enemy:TakeDamage(10)
end
end)
end)
Client/Tool
--Tool/ClientScript
local Player = game:GetService("Players").LocalPlayer
local mouse = Player:GetMouse()
local tool = script.Parent
local ammo = script.Parent.Ammo
local trigger = false
tool.Equipped:Connect(function()
tool.Handle.Equip:Play()
end)
tool.Activated:Connect(function()
if trigger == false then
trigger = true
tool.WeaponFired:FireServer(mouse.Hit.Position)
wait(1)
trigger = false
end
end)