I’ve been making a gun system and I came across a problem where the script waits the wrong time.
Local script:
local Services = {
Players = game:GetService("Players");
ReplicatedStorage = game:GetService("ReplicatedStorage");
UserInputService = game:GetService("UserInputService");
TweenService = game:GetService("TweenService");
}
local Player = Services.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")
local Camera = workspace.CurrentCamera
local CameraMinZoomDistance = Player.CameraMinZoomDistance
local CameraMaxZoomDistance = Player.CameraMaxZoomDistance
local Mouse = Player:GetMouse()
local DefaultMouseIcon = Mouse.Icon
local AimDistance = 10
local UnaimDistance = 16
local Status = {
isEquipped = false;
isAimming = false;
canShoot = true;
}
local Folders = {
RemoteEvents = Services.ReplicatedStorage.Remotes.Events;
RemoteFunctions = Services.ReplicatedStorage.Remotes.Functions;
}
local Remotes = {
DetectGun = Folders.RemoteFunctions.DetectGun;
FireGun = Folders.RemoteFunctions.FireGun;
GetGunSettings = Folders.RemoteFunctions.GetGunSettings;
}
local Sounds = {
AimDownSound = script.AimDownSound;
AimUpSound = script.AimUpSound;
}
local Tweens = {
AimInTween = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0);
AimOutTween = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0);
}
local CameraTweenAim = Services.TweenService:Create(Player, Tweens.AimInTween, {CameraMinZoomDistance = AimDistance, CameraMaxZoomDistance = AimDistance})
local CameraTweenUnAim = Services.TweenService:Create(Player, Tweens.AimOutTween, {CameraMinZoomDistance = UnaimDistance, CameraMaxZoomDistance = UnaimDistance})
local tool = nil
local GunSettings = {}
Character.ChildAdded:Connect(function(Obj)
local IsGun = Remotes.DetectGun:InvokeServer(Obj)
print(IsGun)
if IsGun then
GunSettings = Remotes.GetGunSettings:InvokeServer(Obj)
print(GunSettings)
Status.isEquipped = true
tool = Obj
print("Equipped "..Obj.Name)
end
end)
Character.ChildRemoved:Connect(function(Obj)
local IsGun = Remotes.DetectGun:InvokeServer(Obj)
print(IsGun)
if IsGun then
GunSettings = nil
Status.isEquipped = false
tool = nil
print("Unequipped "..Obj.Name)
end
end)
Services.UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and Status.isEquipped and Status.canShoot then
Status.canShoot = false
local new = os.time()
print(new)
local FiredGun = Remotes.FireGun:InvokeServer(tool)
task.wait(GunSettings["FireRate"])
Status.canShoot = true
print(os.time() - new)
end
if input.UserInputType == Enum.UserInputType.MouseButton2 and Status.isEquipped then
Status.isAiming = true
CameraTweenAim:Play()
Sounds.AimDownSound:Play()
end
end)
Services.UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 and Status.isEquipped then
Status.isAiming = false
CameraTweenUnAim:Play()
Sounds.AimUpSound:Play()
end
end)
Server script (only remotefunction function):
Remotes.GetGunSettings.OnServerInvoke = function(player, Obj)
local Settings = Modules.Settings[Obj.Name]
return Settings
end
Module:
local Settings = {}
Settings["FN FIVE-SEVEN"] = {
HeadMultiplier = 2;
Damage = 10;
BulletSpeed = 500;
FireRate = 0.6;
}
return Settings
Output:
