Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

0

use remote events/functions

1 answer

✓ Accepted answer

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.

2 comments

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?

0

Youtube tutorials is your friend.

This discussion is preserved as it was. New answers, comments, and votes are closed.