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)

1 comment
use remote events/functions