Hey! So for my medieval combat game, i obviously need some bows! I eventually got one working, but it really lags when shooting it and with more than 1 or 2 players, its REALLY bad with the bows, animations bug out, u cant see the arrow trails, sometimes cant shoot at all! Any help?
Local script inside of the bow:
local remote = script.Parent:WaitForChild("Update")
local tool = script.Parent
local camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local owner = ""
tool.Activated:Connect(function()
tool.Parent:FindFirstChild("Humanoid").WalkSpeed = 4
remote:FireServer('Activated')
end)
tool.Deactivated:Connect(function()
tool.Parent:FindFirstChild("Humanoid").WalkSpeed = 16
remote:FireServer('Deactivated', camera.CFrame.LookVector , camera.CFrame.RightVector)
end)
tool.Equipped:Connect(function()
owner = tool.Parent.Name
remote:FireServer('Equipped')
end)
tool.Unequipped:Connect(function()
remote:FireServer('Unequipped')
Players:FindFirstChild(owner).Character:FindFirstChild("Humanoid").WalkSpeed = 16
end)
Server Script in the bow:
-- Setting --
local Damage = script.Parent.Config.Damage.Value
local cooldown = script.Parent.Config.Cooldown.Value
local Chargetime = script.Parent.Config.FullChargeTime.Value
local ArrowVelocity = script.Parent.Config.ArrowVelocity.Value
local minChargeTime = script.Parent.Config.MinChargeTime.Value
local ArrowLife = 30
-- Animation Ids -
local HoldingAnimationId = "http://www.roblox.com/asset/?id=16185149749"
local ChargingAnimationId = "http://www.roblox.com/asset/?id=16199426932"
local ChargedAnimationId = "http://www.roblox.com/asset/?id=16185216323"
local FireAnimationId = "http://www.roblox.com/asset/?id=16184482608"
-- Variable --
local remote = script.Parent:WaitForChild("Update")
local LastFire = tick()
local ChargeStart = tick()
local tool = script.Parent
local Player = tool.Parent.Parent
local Character = Player.Character
local Humanoid = Character:FindFirstChild("Humanoid")
local isCharging = false
local Arrow =game.ReplicatedStorage:WaitForChild("Arrow")
-- Anims --
local Anim = Instance.new("Animation")
Anim.AnimationId = HoldingAnimationId
local HoldingAnim = Humanoid:LoadAnimation(Anim)
local Anim = Instance.new("Animation")
Anim.AnimationId = ChargingAnimationId
local CharginAnim = Humanoid:LoadAnimation(Anim)
local Anim = Instance.new("Animation")
Anim.AnimationId = ChargedAnimationId
local ChargedAnim = Humanoid:LoadAnimation(Anim)
local Anim = Instance.new("Animation")
Anim.AnimationId = FireAnimationId
local FireAnim = Humanoid:LoadAnimation(Anim)
local DrawSound = script.Parent.BowDraw
-- Script --
remote.OnServerEvent:Connect(function(plr, Type, LookVector, RightVector)
if Type == "Unequipped" then
print(("UNEQUIPPING BOW"))
HoldingAnim.Looped = false
DrawSound:Stop()
HoldingAnim:Stop()
HoldingAnim.Looped = false
isCharging = false
script.Parent.Arrow.Transparency = 1
CharginAnim:Stop()
ChargedAnim:Stop()
end
if plr.Name == tool.Parent.Name then
if Type == 'Activated' then
if tick()-LastFire > cooldown then
script.Parent.Arrow.Transparency = 0
ChargeStart = tick()
isCharging = true
HoldingAnim:Stop()
CharginAnim:Play()
DrawSound:Play()
CharginAnim.Stopped:wait()
ChargedAnim:Play()
end
elseif Type == "Deactivated" then
DrawSound:Stop()
script.Parent.Arrow.Transparency = 1
HoldingAnim:Play()
ChargedAnim:Stop()
if tick()-ChargeStart > minChargeTime then
local SoundEffect = Instance.new("Sound")
SoundEffect.SoundId = "rbxassetid://173979241"
SoundEffect.Name = "DeathSoundEffect"
SoundEffect.Volume = 1
SoundEffect.PlaybackSpeed = 1
SoundEffect.SoundGroup = nil
SoundEffect.PlayOnRemove = false
SoundEffect.Looped = false
SoundEffect.RollOffMaxDistance = 10000
SoundEffect.RollOffMinDistance = 10
SoundEffect.TimePosition = 0
SoundEffect.Archivable = false
SoundEffect.Parent = script.Parent:FindFirstChild("Union")
SoundEffect:Play()
--FireArrow
local A = Arrow:Clone()
if not A then
return
else
if A.Sender then
A.Sender.Value = Character
A.Parent = game.Workspace
A.CFrame = script.Parent.Arrow.CFrame
A.Position = A.Position + RightVector*2
else
A.Parent = game.Workspace
A.CFrame = script.Parent.Arrow.CFrame
end
end
-- Calculate Velocity and damage
if tick()-ChargeStart < Chargetime then
A:ApplyImpulse(LookVector*(tick()-ChargeStart)*ArrowVelocity)
A.Damage.Value = Damage*(tick()-ChargeStart)
else
A.Damage.Value = Damage
A:ApplyImpulse(LookVector*Chargetime*ArrowVelocity)
end
--Deletion
end
isCharging = false
elseif Type == "Equipped" then
HoldingAnim:Play()
HoldingAnim.Looped = true
end
end
end)
Server Script inside of the arrow:
local Arrow = script.Parent
local Sender = Arrow.Sender
local Damage = Arrow.Damage
local RS = game:GetService("RunService")
local LastPosition = Arrow.Position
local Players = game:GetService("Players")
local candamage = true
local Stuck = game.ReplicatedStorage.ArrowStuck
local tweenservice = game:GetService("TweenService")
local tweens = {}
local indicators = {}
local function SyncTweenFrames(frames, property, Time)
for _, frame in frames do
local tween = tweenservice:Create(frame, TweenInfo.new(Time), property)
tween:Play()
table.insert(tweens, tween)
end
end
function Hitmarker(hit,dmg)
local plr = Players:FindFirstChild(Sender.Value.Name)
local plrgui = Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui")
for index, tween in tweens do
tween:Pause()
table.remove(tweens, index)
end
SyncTweenFrames({
plrgui.Markers.Wrapper.First,
plrgui.Markers.Wrapper.Second,
plrgui.Markers.Wrapper.Third,
plrgui.Markers.Wrapper.Fourth,
},
{BackgroundTransparency = 0}, 0)
plrgui.Markers.Wrapper.Hit.TextTransparency = 0
plrgui.Markers.Wrapper.First.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
plrgui.Markers.Wrapper.Second.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
plrgui.Markers.Wrapper.Third.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
plrgui.Markers.Wrapper.Fourth.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
plrgui.Markers.Wrapper.Hit.Text = hit.Parent.Name.."'s "..hit.Name.." for \n"..math.floor(dmg)
wait(0.4)
SyncTweenFrames({
plrgui.Markers.Wrapper.First,
plrgui.Markers.Wrapper.Second,
plrgui.Markers.Wrapper.Third,
plrgui.Markers.Wrapper.Fourth,
},
{BackgroundTransparency = 1}, 0.3)
SyncTweenFrames({plrgui.Markers.Wrapper.Hit}, {TextTransparency = 1}, 0.3)
end
function KillMarker(plrN)
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("First").BackgroundTransparency = 0
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Second").BackgroundTransparency = 0
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Third").BackgroundTransparency = 0
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Fourth").BackgroundTransparency = 0
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Hit").TextTransparency = 0
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Skull").ImageTransparency = 0
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("First").BackgroundColor3 = Color3.fromRGB(255, 0, 0)
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Second").BackgroundColor3 = Color3.fromRGB(255, 0, 0)
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Third").BackgroundColor3 = Color3.fromRGB(255, 0, 0)
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Fourth").BackgroundColor3 = Color3.fromRGB(255, 0, 0)
Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Hit").Text = plrN
end
function HitSound(parent)
local SoundEffect = Instance.new("Sound")
SoundEffect.SoundId = "rbxassetid://5473058688"
SoundEffect.Name = "DeathSoundEffect"
SoundEffect.Volume = 1
SoundEffect.PlaybackSpeed = 1
SoundEffect.SoundGroup = nil
SoundEffect.PlayOnRemove = false
SoundEffect.Looped = false
SoundEffect.RollOffMaxDistance = 10000
SoundEffect.RollOffMinDistance = 10
SoundEffect.TimePosition = 0
SoundEffect.Archivable = false
SoundEffect.Parent = parent
SoundEffect:Play()
end
function ImpactSound(parent)
local SoundEffect = Instance.new("Sound")
SoundEffect.SoundId = "rbxassetid://16185680893"
SoundEffect.Name = "DeathSoundEffect"
SoundEffect.Volume = 1
SoundEffect.PlaybackSpeed = 1
SoundEffect.SoundGroup = nil
SoundEffect.PlayOnRemove = false
SoundEffect.Looped = false
SoundEffect.RollOffMaxDistance = 10000
SoundEffect.RollOffMinDistance = 10
SoundEffect.TimePosition = 0
SoundEffect.Archivable = false
SoundEffect.Parent = parent
SoundEffect:Play()
end
RS.Heartbeat:Connect(function()
local Ignore = {Sender.Value, Arrow}
for i, Part in pairs(workspace:GetDescendants()) do
if Part:IsA("BasePart") then
if not Part.Parent:FindFirstChildOfClass("Humanoid") and not Part.CanCollide then
table.insert(Ignore, Part)
end
end
end
local params = RaycastParams.new()
params.FilterDescendantsInstances = Ignore
params.FilterType = Enum.RaycastFilterType.Blacklist
local results = workspace:Raycast(LastPosition, Arrow.Position-LastPosition, params)
if results then
print("INSTANCE: "..results.Instance.Name)
if results.Instance.Parent:FindFirstChildOfClass("Humanoid") then
local hum = results.Instance.Parent:FindFirstChildOfClass("Humanoid")
local hit = results.Instance
local dmg = 1
if candamage == true then
candamage = false
task.wait(0.1)
if hum.Health > (dmg*Damage.Value) then
hum.Health = hum.Health - (dmg*Damage.Value)
else
hum.Health = hum.Health - (dmg*Damage.Value)
KillMarker(hum.Parent.Name)
end
Hitmarker(hit, (dmg*Damage.Value))
ImpactSound(hit)
local StuckClone = Stuck:Clone()
StuckClone.CFrame = Arrow.CFrame
StuckClone.Position = results.Position
StuckClone.Parent = results.Instance
local weld = Instance.new("WeldConstraint")
weld.Parent = results.Instance
weld.Part0 = StuckClone
weld.Part1 = results.Instance
game.Debris:AddItem(StuckClone, 7)
Arrow.Trail.Enabled =false
Arrow.Transparency = 1
end
else
if candamage == true then
local StuckClone = Stuck:Clone()
candamage =false
StuckClone.Parent = workspace.Arrows
StuckClone.CFrame = Arrow.CFrame
StuckClone.Position = results.Position
StuckClone.Anchored = true
game.Debris:AddItem(StuckClone,5)
task.wait(0.1)
Arrow.Trail.Enabled =false
Arrow.Transparency = 1
end
end
end
LastPosition = Arrow.Position
end)