Hello everyone! I’m trying to implement remote events and module scripts for projectile rendering and hit detection.
So far I’ve managed to implement a throwing weapon that moves and behaves almost exactly as I want it to. However, the system I got running is pretty primitive and easily exploitable because of being handled by a single on server event.
I understand that velocity and trajectory should probably go to a module script, but where is best to put it and how to connect it with server and client without overcomplicating the script?
So, my questions are: how to use remote events and module scripts in this one? How to wrap damage and rendering into modules and where to put them - server or replicated?
-- Tool controller
local Player = game:GetService("Players").LocalPlayer
local mouse = Player:GetMouse()
local tool = script.Parent
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)
-- Server Script
local Debris = game:GetService("Debris")
local rep = game:GetService('ReplicatedStorage')
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)
--- calculate time, trajectory and velocity here and throw the projectile
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)