Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I modify this local script so that Its viewable In the server-side?

Asked by 1 year ago

Hello Developers!

I made a local-script Inside of StarterPlayerServices which allows the user to see what weapon they have on a first person view (Basically, a First Person Shooter Game) with the help of this tutorial: https://www.youtube.com/watch?v=c96NS9dxP60 (There Is multiple parts, around 5.5) The Issue Is that I want every player In the server to see what gun you have, as well of you being able to see the other players weapons, however, I currently don't know how to do this

Script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local aimCFrame = CFrame.new()
local IsAiming = false
local IsShooting = false
local IsReloaing = false
local CanShoot = true
local currentSwayAMT = .2
local SwayAMT = .2
local aimSwayAMT = .2
local SwayCF = CFrame.new()
local Mouse = Player:GetMouse()
local lastCameraCF = CFrame.new()
local Fireanim = nil
local Firesound = nil
local Frameworks = {
    Inventory = {
        "XLS Mark"
    };
    module = nil;
    viewmodel = nil;
    CurrentWeapon = 1;
}

function loadSlot(Item)
    if IsReloaing == false then
        local viewModelFolder = game.ReplicatedStorage.ViewModels
        local moduleFolder = game.ReplicatedStorage.Modules

        for i,v in pairs(Camera:GetChildren()) do
            if v:IsA("Model") then
                v:Destroy()
            end
        end

        if moduleFolder:FindFirstChild(Item) then
            Frameworks.module = require(moduleFolder:FindFirstChild(Item))


            if viewModelFolder:FindFirstChild(Item) then
                Frameworks.viewmodel = viewModelFolder:FindFirstChild(Item):Clone()
                Frameworks.viewmodel.Parent = Camera

                if Frameworks.viewmodel and Frameworks.module and Character then
                    Fireanim = Instance.new("Animation")
                    Fireanim.Parent = Frameworks.viewmodel
                    Fireanim.Name = "Fire"
                    Fireanim.AnimationId = Frameworks.module.fireAnim
                    Fireanim = Frameworks.viewmodel.AnimationController.Animator:LoadAnimation(Fireanim)
                    game.ReplicatedStorage.Events.LoadSlot:FireServer(Frameworks.module.fireSound.SoundId, Frameworks.module.fireSound.Volume)
                end
            end
        end
    end
end

local OldCamCF = CFrame.new()

function UpdateCamSH()
    local NewCamCF = Frameworks.viewmodel.FakeCam.CFrame:ToObjectSpace(Frameworks.viewmodel.PrimaryPart.CFrame)
    Camera.CFrame = Camera.CFrame * NewCamCF:ToObjectSpace(OldCamCF)
    OldCamCF = NewCamCF
end

local hud = Player.PlayerGui:WaitForChild("HUD")

RunService.RenderStepped:Connect(function()

    local rot = Camera.CFrame:ToObjectSpace(lastCameraCF)
    local X,Y,Z = rot:ToOrientation()
    SwayCF = SwayCF:Lerp(CFrame.Angles(math.sin(X)*currentSwayAMT, math.sin(Y)*currentSwayAMT, 0), .1)
    lastCameraCF = Camera.CFrame

    if Frameworks.viewmodel and Frameworks.module then
        hud.GunName.Text = "Current weapon: "..Frameworks.Inventory[Frameworks.CurrentWeapon]
        hud.AMMO.Text = "AMMO: "..Frameworks.module.Ammo.."/"..Frameworks.module.MaxAmmo
    end

    local Humanoid = Character:WaitForChild("Humanoid")

    if Humanoid then
        local Boboffset = CFrame.new()
        if Humanoid.MoveDirection.Magnitude > 0 then
            if Humanoid.WalkSpeed == 16 then
                Boboffset = CFrame.new(math.cos(tick() * 5) * .1, -Humanoid.CameraOffset.Y/3, -Humanoid.CameraOffset.Z/3) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05)

            elseif Humanoid.WalkSpeed >= 24 then
                Boboffset = CFrame.new(math.cos(tick() * 9) * .15, -Humanoid.CameraOffset.Y/3, -Humanoid.CameraOffset.Z/3) * CFrame.Angles(math.cos(tick() * 4) * .05, math.sin(tick() * 8) * .1, 0)
            end
        else
            Boboffset = Boboffset:Lerp(CFrame.new(0, -Humanoid.CameraOffset.Y/3, 0), .1)
        end


        for i,v in pairs(Camera:GetChildren()) do
            if v:IsA("Model") then
                v:PivotTo(Camera.CFrame * SwayCF * aimCFrame * Boboffset)
                --UpdateCamSH()
            end
        end
    end

    if IsAiming and Frameworks.viewmodel ~= nil and Frameworks.module.canAim then
        local offset = Frameworks.viewmodel.AimPart.CFrame:ToObjectSpace(Frameworks.viewmodel.PrimaryPart.CFrame)
        aimCFrame = aimCFrame:Lerp(offset, Frameworks.module.aimSmooth)
        currentSwayAMT = aimSwayAMT
    else
        local offset = CFrame.new()
        aimCFrame = aimCFrame:Lerp(offset, Frameworks.module.aimSmooth)
        currentSwayAMT = SwayAMT
    end
end)

loadSlot(Frameworks.Inventory[1])

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.One then
        if Frameworks.CurrentWeapon ~= 1 and IsReloaing == false then
            loadSlot(Frameworks.Inventory[1])
            Frameworks.CurrentWeapon = 1
        end
    end

    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        IsAiming = true
    end

    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if Character and Frameworks.viewmodel and Frameworks.module and Frameworks.module.Ammo ~= 0 then
            if Frameworks.module.fireMode == "Semi" then
                Fireanim:Play()
                game.ReplicatedStorage.Events.Shoot:FireServer()
                Frameworks.module.Ammo -= 1
            end

            if Frameworks.module.fireMode == "Full Auto" then
                IsShooting = true
            end
        end
    end

    if input.KeyCode == Enum.KeyCode.R then
        IsReloaing = true
        CanShoot = false
        task.wait(Frameworks.module.ReloadingTime)
        Frameworks.module.Ammo = Frameworks.module.MaxAmmo
        IsReloaing = false
        CanShoot = true
    end

    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if Character and Frameworks.viewmodel and Frameworks.module and Frameworks.module.Ammo == 0 then
            IsReloaing = true
            CanShoot = false
            task.wait(Frameworks.module.ReloadingTime)
            Frameworks.module.Ammo = Frameworks.module.MaxAmmo
            IsReloaing = false
            CanShoot = true
        end
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        IsAiming = false
    end
end)

while wait() do
    if IsShooting and Frameworks.module.Ammo > 0 then
        game.ReplicatedStorage.Events.Shoot:FireServer()
        Fireanim:Play()
        Frameworks.module.Ammo -= 1
        Mouse.Button1Up:Connect(function()
            IsShooting = false
        end)
        wait(Frameworks.module.fireRate)
    end
end

1 answer

Log in to vote
0
Answered by 1 year ago

Fire a RemoteEvent when you are holding the gun. I think it should be at line 120 when the input began on Enum.KeyCode.One.

So when the input begins, fire a RemoteEvent with the variables as the gun.

Then on a server Script

remoteEvent.OnServerEvent:Connect(function(player, gun)
  -- equip gun or something
end)

I hope this is what you're looking for. I typed everything on mobile at 3am so beware of errors.

Ad

Answer this question