I’ve decided that I want all my projectile logic on the client and just have the server deal damage. Here’s why:
As you can see the projectile has a noticeable delay when fired and then actually moving. Also, I don’t have a video of it, but the game gets pretty laggy when their are more players.
I have created a simple script, but I don’t know the best way to replicate the projectile for all the clients.
local script:
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RE = game.ReplicatedStorage:WaitForChild("Magic")
local canuse = true
local character = player.Character
local debris = game:GetService("Debris")
UIS.InputBegan:Connect(function(key, chat)
if key.KeyCode == Enum.KeyCode.E and not chat then
local sound = Instance.new("Sound")
sound.PlayOnRemove = true
sound.Volume = 2
sound.SoundId = "rbxassetid://144699494"
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(2,2,2)
part.BrickColor = BrickColor.new("Bright red")
part.TopSurface = Enum.SurfaceType.Smooth
part.BottomSurface = Enum.SurfaceType.Smooth
part.CanCollide = false
part.CFrame = CFrame.new(character:FindFirstChild("HumanoidRootPart").Position, mouse.Hit.p)
sound.Parent = part
local bodyv = Instance.new("BodyVelocity")
bodyv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyv.Velocity = part.CFrame.LookVector * 100
bodyv.Parent = part
part.Parent = workspace.Projectiles
part.Touched:Connect(function(touch) --I know touch events are bad but I couldn't think of a performant alternative (maybe raycasting or magnitude)
if not touch:IsDescendantOf(character) then
RE:FireServer(touch.Parent)
part:Destroy() -- I know an exploiter could get rid of this but I dont know how to fix it
end
end)
end
end)
Here is the Server:
local RE = game.ReplicatedStorage:WaitForChild("Magic")
RE.OnServerEvent:Connect(function(player, enemy)
if player.character:FindFirstChildWhichIsA("Humanoid").Health > 0 and enemy:FindFirstChildWhichIsA("Humanoid") then
enemy:FindFirstChildWhichIsA("Humanoid"):TakeDamage(35)
end
end)
Now my question is, how do I allow it so all clients can see the projectile?