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

How do I change this code to be in a regular script?

Asked by 5 years ago

I've written a code for a gun with help of google, changing some things and adding unstable fixes to problems. I don't know how to change this to a regular script instead of local script because the effects like damage, muzzle flash etc. don't appear on the server but only on the client, and other players don't see it. Help me, please. Code:

wait(1)
local FIRE_DELAY = 0.09

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local idle = script.Parent:WaitForChild("Idle")
local Idle = player.Character.Humanoid:LoadAnimation(idle)

tool.Equipped:connect(function(mouse)
    Idle.Looped = true
    Idle:Play()
    script.Parent.Equip:Play()
    print("Tool equipped!")

    tool.Unequipped:Connect(function()

        script.Parent.Equip:Play()
        local Humanoid = game.Players.LocalPlayer.Character.Humanoid
        local ActiveTracks = Humanoid:GetPlayingAnimationTracks()
        for _,v in pairs(ActiveTracks) do
            v:Stop()
    local clone = script:Clone()
    clone.Parent = script.Parent -- Copy the script and destroy the broken one
    script:Destroy()
        end

    end)

local active = false
tool.Activated:Connect(function()
    script.Parent.Handle.Particles.ParticleEmitter.Transparency = NumberSequence.new(1,0.4,0)
    script.Parent.Handle.Particles.Smoke.Transparency = NumberSequence.new(1,0.8,0)
    active = true
end)

tool.Deactivated:Connect(function()
    script.Parent.Handle.Particles.ParticleEmitter.Transparency = NumberSequence.new(1,1,0)
    script.Parent.Handle.Particles.Smoke.Transparency = NumberSequence.new(1,1,0)
    active = false
end)

local function CanShoot()
    return active
end

local function Shoot()
    -- Shooting code goes here.
    script.Parent.Shot:Play() -- PLAY SOUND
    if not mouse then
        wait(0.1)
    end
    local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300) -- MAKE A RAY

    game:GetService("ScriptContext").Error:Connect(function(msg,stack,source) -- Here's a tricky way to handle script error
    print(msg,stack,source.Name)
    local clone = script:Clone()
    clone.Parent = script.Parent -- Copy the script and destroy the broken one
    script:Destroy()

    end)
    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
    if part then -- IF THE RAY HIT SOMETHING
        print(part.Name)
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if not humanoid then
            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
        end

        if humanoid then
            print("Humanoid found")
            humanoid.Health = humanoid.Health - 20
        end
    end

end

local lastFiredTime = tick()
game:GetService("RunService").Stepped:Connect(function()
    local timeNow = tick()
    if CanShoot() then
        if timeNow - lastFiredTime > FIRE_DELAY then
            Shoot()
            lastFiredTime = timeNow
        end
    end
end)

end)

0
use remote events/functions DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

First, stuff like gun sound, effects, damage and all that effects that should show up on every client, you should put it all in the gun's script. You can have the fuctions in a script under the gun object and have it connected with a remote event or remote fuction that is triggered by the player clicking the mouse while holding the gun.

When you are inserting an object you have more options than just a localscript, there is also a regular script.

0
But, how do I do it? You just told me to move some of the localscript to a regular script and connect the two with a remote event. I'm a beginner, could you explain what should I do? DrewnoBrzozowe 24 — 5y
0
Youtube tutorials is your friend. Silvanatri 24 — 5y
Ad

Answer this question