Hello dev forum!
My game Prism is playable now but there is an issue:
- There is too much delay when playing while in studio, there isn’t any lag
I don’t know if it the server lag, or is it my script that is bad. Help is appreciated!
Code
- Client
--Services
local ReplicatedStorage = game.ReplicatedStorage
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
--Player and Character
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local Health = Humanoid.Health
local Camera = workspace.CurrentCamera
--GUI
local PlayerGui = Player:WaitForChild("PlayerGui")
--Gun Parts
local BulletHole = script.Parent.Parent:WaitForChild("BulletHole")
local MuzzleFlash = BulletHole:WaitForChild("MuzzleFlash")
--Tool Stuff
local Tool = script.Parent.Parent.Parent
--Values
local ReloadTime = Values:WaitForChild("ReloadTime")
local Damage = Values:WaitForChild("Damage")
local HeadshotMulti = Values:WaitForChild("HeadshotMulti")
local CurrentAmmo = Values:WaitForChild("CurrentAmmo")
local ReserveAmmo = Values:WaitForChild("ReserveAmmo")
local MaxAmmo = Values:WaitForChild("MaxAmmo")
--Sounds
local ReloadSound = Sounds:WaitForChild("Reload")
local ShootSound = Sounds:WaitForChild("Shoot")
local HeadshotSound = Sounds:WaitForChild("Headshot")
--Animations
local ReloadAnim = script.Parent:WaitForChild("Animations").Reload
local RecoilAnim = script.Parent:WaitForChild("Animations").Recoil
local HoldAnimation = script.Parent:WaitForChild("Animations").Hold
--EventsFolder
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local RemoteFunctions = ReplicatedStorage:WaitForChild("RemoteFunctions")
--Functions and events
local ReloadFunction = RemoteFunctions:WaitForChild("Reload")
local ShootEvent = RemoteEvents:WaitForChild("Shoot")
local CanShootFunction = RemoteFunctions:WaitForChild("CanShoot")
local ReloadEvent = RemoteEvents:WaitForChild("Reload")
local TrailEvent = RemoteEvents:WaitForChild("MakeTrail")
--In script Values
local mouseDown = false
local shooting = false
local equipped = false
local reloading = false
local FireMode = "Auto"
local db = false
local hold
local function fire()
local result = CanShootFunction:InvokeServer(CurrentAmmo)
if not reloading then
if result == true then
character:WaitForChild("Humanoid"):LoadAnimation(RecoilAnim):Play()
shooting = true
local pos1 = BulletHole.Position
local pos2 = Mouse.Hit.p
TrailEvent:FireServer(pos1,pos2,nil,BrickColor.new("Toothpaste"))
--
local ray = Ray.new(pos1,(pos2-pos1).Unit * 300)
local touched, direction = workspace:FindPartOnRay(ray,Player.Character,false,true)
local Dist = (pos1-pos2).magnitude
if touched then
ShootEvent:FireServer(touched,Damage,ShootSound,CurrentAmmo,HeadshotMulti,MuzzleFlash,direction)
end
end
end
end
--
Tool.Activated:Connect(function()
if Humanoid.Health <= 0 then return end
mouseDown = true
if not db then
if reloading == false then
db = true
shooting = true
if FireMode == "Auto" then
repeat fire()
wait(0.04)
until not mouseDown
wait(.02)
shooting = false
db = false
elseif FireMode == "Semi" then
fire()
wait(0.2)
shooting = false
db = false
elseif FireMode == "Slow-Semi" then
fire()
wait(2)
shooting = false
db = false
end
end
end
end)
--
Mouse.Button1Up:Connect(function()
mouseDown = false
end)
--
Tool.Equipped:Connect(function()
equipped = true
GunGui.Enabled = true
Mouse.Icon = "rbxassetid://316279304"
Player.CameraMode = Enum.CameraMode.LockFirstPerson -- force first person
Camera.FieldOfView = 100
Humanoid.CameraOffset = Vector3.new(0, 0.2, -.5)
--
game.ReplicatedStorage.RemoteEvents.ConnectM6D:FireServer(script.Parent)
AmmoAmountLabel.Text = CurrentAmmo.Value.."/"..ReserveAmmo.Value
--
character.Torso.ToolGrip.Part0 = character.Torso
character.Torso.ToolGrip.Part1 = script.Parent
--
hold = character:WaitForChild("Humanoid"):LoadAnimation(HoldAnimation)
hold:Play()
end)
--
Tool.Unequipped:Connect(function()
Mouse.Icon = "rbxassetid://5675412080"
equipped = false
GunGui.Enabled = false
Player.CameraMode = Enum.CameraMode.Classic -- force first person
Camera.FieldOfView = 70
Humanoid.CameraOffset = Vector3.new(0, 0, 0)
game.ReplicatedStorage.RemoteEvents.DisconnectM6D:FireServer()
hold:Stop()
end)
- Server
ShootEvent.OnServerEvent:Connect(function(player,hit,damage,sound,ammo,multiplier,MuzzleFlash,direction)
sound:Play()
ammo.Value = ammo.Value - 1
if MuzzleFlash then
MuzzleFlash.Enabled = true
wait(0.006)
MuzzleFlash.Enabled = false
end
if hit then
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum ~= nil then
print(hum)
if hit.Name == "Head" or hit:IsA("Accessory") or hit.Parent:IsA("Accessory") then
hum:TakeDamage(damage.Value * multiplier.Value)
else
hum:TakeDamage(damage.Value)
end
end
end
end)
ReloadFUnction.OnServerInvoke = function(plr,ammo,reserveAmmo,sound,reloadtime,default,anim,fx)
if ammo.Value ~= default then
if reserveAmmo.Value >= default then
if reloading == false then
reloading = true
plr.Character:FindFirstChild("Humanoid"):LoadAnimation(anim):Play()
sound:Play()
if fx then
wait(reloadtime/2)
fx:Emit(100)
wait(reloadtime/2)
return true
else
wait(reloadtime)
return true
end
end
end
end
return false
end
CanShootFUnction.OnServerInvoke = function(player, ammo)
if ammo.Value > 0 and reloading == false then
return true
else
return false
end
end
TrailEvent.OnServerEvent:Connect(function(Player,pos1,pos2,MuzzleFlash,brickColor)
local ray = Ray.new(pos1,(pos2-pos1).Unit * 200)
local touched, direction = workspace:FindPartOnRay(ray,Player.Character,false,true)
local Dist = (pos1-pos2).magnitude
local Lazer = Instance.new("Part")
Lazer.Parent = game.Workspace
Lazer.Anchored = true
Lazer.CanCollide = false
Lazer.Size = Vector3.new(0.05,0.05,Dist)
Lazer.CFrame = CFrame.new(pos1,direction)*CFrame.new(0,0,-Dist/2)
Lazer.Material = Enum.Material.Neon
if brickColor then
Lazer.BrickColor = brickColor
else
Lazer.BrickColor = BrickColor.new("Institutional white")
end
local ti = TweenInfo.new(0.3,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
local Goals = {Transparency = 1}
TweenService:Create(Lazer,ti,Goals):Play()
Debris:AddItem(Lazer,0.3)
end)
-
Explorer
Replicated Storage
Gun
Help is needed!
Thanks