Hello,
I am creating a gun but I have a problem, my animations are not played instantly once the gun is in my character…
local rs = game:GetService("ReplicatedStorage")
local rf = game:GetService("ReplicatedFirst")
local ContextActionService = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local TweenS = game:GetService("TweenService")
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
repeat wait(1) until plr.Character ~= nil
local humanoid = plr.Character:FindFirstChildOfClass("Humanoid")
local mouse = plr:GetMouse()
local GUI = script.Parent.ViperUI
local MaxAmmo = script.Parent.MaxAmmo
local CurrentAmmo = script.Parent.CurrentAmmo
local ShootPosition = script.Parent.ShootPosition
local SoundGunShot = script.Parent.GunFire
local ColorAmmo = script.Parent.ColorAmmo
local DamageAmmo = script.Parent.DamageAmmo
local tool = script.Parent
local equipped = false
local inprotect = false
local fired = false
local endreloaded = true
local ACTION_RELOAD = "Reload"
local CAMERA = workspace.CurrentCamera
local event_gun_shot = rs.Events.GunShot
local function loadanimation(anim)
return humanoid.Animator:LoadAnimation(anim)
end
local Anim = {
Equipped = loadanimation(script.Parent.EquipAnim),
Protect = loadanimation(script.Parent.Protect)
}
tool.Equipped:Connect(function()
equipped = true
Anim.Equipped:Play()
GUI.Parent = plr.PlayerGui
GUI.Enabled = true
UIS.InputBegan:Connect(function(key, _gameProcess)
if key.KeyCode == Enum.KeyCode.F and not _gameProcess then
if not Anim.Protect.IsPlaying then
inprotect = true
Anim.Protect:Play()
Anim.Equipped:Stop()
else
inprotect = false
Anim.Protect:Stop()
Anim.Equipped:Play()
end
end
end)
mouse.Button1Down:Connect(function()
if equipped and not inprotect then
fired = true
while fired do
if endreloaded then
if CurrentAmmo.Value > 0 then
SoundGunShot:Play()
CurrentAmmo.Value -= 1
event_gun_shot:FireServer(ShootPosition.CFrame.p, mouse.Hit.p, DamageAmmo.Value, ColorAmmo.Value)
end
end
wait(0.035)
end
end
end)
mouse.Button1Up:Connect(function() if equipped then fired = false end end)
local function handleAction(actionName, inputState, inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
if endreloaded and CurrentAmmo.Value < 50 then
endreloaded = false
local realoadFolder = tool.Reload
local function updateTransparency(folder, value, mode)
if mode == "Tween" then
for i,v in ipairs(folder:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("Part") then
v.Transparency = 0
local anim = TweenS:Create(v, TweenInfo.new(0.5), {Transparency = value})
anim:Play()
wait(0.005)
end
end
else
for i,v in ipairs(folder:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("Part") then
v.Transparency = value
end
end
end
end
updateTransparency(tool, 1, "normal")
updateTransparency(realoadFolder, 1, "Tween")
wait(0.5)
updateTransparency(realoadFolder, 0, "normal")
wait(0.5)
updateTransparency(realoadFolder, 1, "normal")
updateTransparency(tool, 0, "normal")
ShootPosition.Transparency = 1
CurrentAmmo.Value = MaxAmmo.Value
endreloaded = true
end
end
end
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
equipped = false
GUI.Parent = tool
GUI.Enabled = false
fired = false
Anim.Protect:Stop()
Anim.Equipped:Stop()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)