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

My gun is working fine in studio mode, but it isn't working ingame. How could I fix this?

https://gyazo.com/a5c8f8ba378c63f9464e0f8be70cace8

This is my gun, it has only two scripts in it. One is for the weld and one is for the gun.

When I play it in studio it works perfectly fine! Nothing wrong with it at all. But ingame it doesn't weld and instead holds it with one hand and it cannot shoot or do anything.

I looked at both of the scripts and now they are localscripts but it still doesn't work.

Here is the script for the weld.

local tool = script.Parent
local parts = script.Parent:GetChildren()
function weld()
	for i = 1,#parts do
		if ((parts[i].className == "Part") or (parts[i].className == "Seat") or (parts[i].className == "TrussPart") or (parts[i].className == "VehicleSeat") or (parts[i].className == "SkateboardPlatform")) then
			local weld = Instance.new("Weld")
			weld.Part0 = tool.Handle
			weld.Part1 = parts[i]
			weld.C0 = tool.Handle.CFrame:inverse()
			weld.C1 = parts[i].CFrame:inverse()
			weld.Parent = tool.Handle
			parts[i].Anchored = false
		end
	end
	wait(3)
end

weld()
tool.Equipped:connect(weld)

And this is the script for the gun.

local reloadTime = 2.5  -- Reload time in seconds
local clipSize = 12   -- Size of a clip
local fireRate = .09 -- Time between shots in seconds
local damage = 100     -- Damage per bullet
local spread = 0.4   -- Spread in studs	
local range = 70    -- Range in studs
local ammo = clipSize -- Amount in starting clip

local teamDamageEnabled = true
local serverDamageEnabled = true 
local hitSoundEnabled = true

local cursorNormal = "rbxasset://textures\\GunCursor.png"
local cursorReload = "rbxasset://textures\\GunWaitCursor.png"
local hitSoundId = "rbxassetid://160432334"

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local tool, player = script.Parent, game.Players.LocalPlayer
local character, handle, mouseDown, reloading, firing, debris, cframe, vector3, instance, ray, mathRandom, server, body, config, deathCon, serverDmg, pingCon, create = player.Character, tool.Handle, false, false, false, game:GetService("Debris"), CFrame.new, Vector3.new, Instance.new, Ray.new, math.random, workspace:findFirstChild("Server.rbxl"), {Torso, LeftArm ,RightArm, LeftShoulder, RightShoulder}, assert(LoadLibrary("RbxUtility")).Create
local create = assert(LoadLibrary("RbxUtility")).Create

local weld = instance("Weld")
local leftWeld, rightWeld = weld:Clone(), weld:Clone()

local hitSound = create("Sound"){SoundId = hitSoundId; Pitch = 1; Volume = hitSoundEnabled and 1 or 0}
local laserPart = create("Part"){Name = "_B"; FormFactor = 0; BrickColor = BrickColor.new("white"); Anchored = true, CanCollide = false; Transparency = 0.6; Locked = true; Size = vector3(1,1,1); TopSurface = 0; BottomSurface = 0}
local mesh = create("SpecialMesh"){Parent = laserPart; MeshType = "Brick"; Name = "Mesh"; Scale = vector3(.02, .02, 1)}
local creatorTag = create("ObjectValue"){Name = "creator"; Value = player}

if tool:findFirstChild("Loaded") then
	ammo = 0
else
	local loadedTag = creatorTag:Clone()
	loadedTag.Name = "Loaded"
	loadedTag.Parent = tool
end

serverDmg = serverDamageEnabled and (server and true or false) or false

local function raycast(start, finish, ignore)
	local directionVec = (finish-start).unit
	local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray(start, directionVec*range), ignore)
		if hit and pos and hit.Name=="_B" then		
			hit, pos = raycast(pos, directionVec * range, ignore)
		end
	return hit, pos
end

local function fire(aim, mouse)
	
	local origin = tool.Muzzle.Position
	local distance = (origin-aim).magnitude
	local minSpread, maxSpread = -(spread/100)*distance, (spread/100)*distance
	
	local finalAim = vector3((aim.x)+(mathRandom(minSpread, maxSpread)),
							 (aim.y)+(mathRandom(minSpread, maxSpread)), 
	 						 (aim.z)+(mathRandom(minSpread, maxSpread)))
	
	local hitPart, hitPos = raycast((player.Character.Head.CFrame * CFrame.new(0.5,0,0)).p, finalAim, {player.Character})
	local bulletLength, orientation = (origin-hitPos).magnitude, cframe(origin, hitPos)
	
	local laser1, laser2 = laserPart:clone(), laserPart:clone()
	laser1.CFrame = orientation * cframe(0,0,-bulletLength*.75)
	laser1.Mesh.Scale = vector3(.1, .1 , bulletLength*.5)
	laser2.CFrame = orientation * cframe(0,0, -bulletLength*.25)
	laser2.Mesh.Scale = vector3(.1, .1, bulletLength*.5)	
	laser1.Parent = workspace 
	laser2.Parent = workspace 
	debris:AddItem(laser2, .03)
	debris:AddItem(laser1, .06)
	
	if hitPart then
		local humanoid = hitPart.Parent:FindFirstChild("Humanoid") or hitPart.Parent.Parent:FindFirstChild("Humanoid")
		if humanoid then		
			local target = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			if not teamDamageEnabled and (target and target.TeamColor~=player.TeamColor) or teamDamageEnabled then	
				if not humanoid:findFirstChild("creator") then
					local killTag = creatorTag:clone()
					killTag.Parent = humanoid
					debris:AddItem(killTag, 0.5)
				end		
				if serverDmg then
					server.Hit:FireServer(humanoid, damage)
				else
					humanoid:TakeDamage(damage)
				end			
				hitSound:Play()	
			end		
		end
	end	
	tool.Name = "["..ammo.."]"
end

local function keyPressed(mouse,key)
	if not reloading and key=='r' and ammo~=clipSize then
		handle.Reload:Play()
		mouse.Icon = cursorReload
		tool.Name = "[REL]"
		reloading = true
		wait(reloadTime)
		ammo = clipSize
		mouse.Icon = cursorNormal
		tool.Name = "["..ammo.."]"
		reloading = false
	end
end


local function mouseClicked(mouse)
	mouseDown  = true
	if ammo>0 and not reloading and not firing then
		firing, ammo = true, ammo - 1
		fire(mouse.hit.p, mouse)
		handle.Fire.Pitch = 1 + math.random(-10,20)/100
		handle.Fire:Play()
		local fireclone = handle.Fire:clone()
		fireclone.Parent = handle
		fireclone:Play()
		local muzzleclone = tool.Muzzle.MuzzleFlash.Flash:clone()
		muzzleclone.Parent = tool.Muzzle.MuzzleFlash
		muzzleclone.Visible = true
		muzzleclone.Rotation = math.random(0,359)
		local lightclone = tool.Muzzle.MuzzleLight:clone()
		lightclone.Parent = tool.Muzzle
		lightclone.Enabled = true
		debris:AddItem(muzzleclone, .01)
		debris:AddItem(lightclone, .02)
		debris:AddItem(fireclone, .15)
		wait(fireRate)
		firing = false
	elseif ammo<=0 and not firing then
		keyPressed(mouse, 'r')
	end
end

tool.Equipped:connect(function(mouse)
	if player.Character.Humanoid.Health<=0 then return end
	deathCon = player.Character.Humanoid.Changed:connect(function(property) 		
		if property=="Health" and player.Character.Humanoid.Health<=0 then		
			tool:remove()
		end
	end)
	mouse.Button1Down:connect(function() mouseClicked(mouse) end)
	mouse.Button1Up:connect(function() mouseDown = false end)
	mouse.KeyDown:connect(function(key) keyPressed(mouse, key) end)
	tool.Name = reloading and "[REL]" or "["..ammo.."]"
	mouse.Icon = cursorNormal
	hitSound.Parent = player.PlayerGui
	equipped=true
end)

tool.Unequipped:connect(function()
	mouseDown = false
	deathCon:disconnect()
	hitSound.Parent = nil
	equipped = false
end)

tool.Equipped:connect(function()
	wait()
	body.Torso = tool.Parent:findFirstChild("Torso")
	body.LeftArm, body.RightArm, body.LeftShoulder, body.RightShoulder = tool.Parent:findFirstChild("Left Arm"), tool.Parent:findFirstChild("Right Arm"), body.Torso:findFirstChild("Left Shoulder"), body.Torso:findFirstChild("Right Shoulder")
	if body.LeftArm and body.RightArm and body.Torso and body.LeftShoulder and body.RightShoulder then
		body.LeftShoulder.Part1 = nil
		body.RightShoulder.Part1 = nil
		leftWeld.Part0 = body.Torso
		leftWeld.Parent = body.Torso
		leftWeld.Part1 = body.LeftArm
		leftWeld.C1 = cframe(0.8,0.5,0.4)* CFrame.Angles(math.rad(270),math.rad(40),0)
		rightWeld.Part0 = body.Torso
		rightWeld.Parent = body.Torso		
		rightWeld.Part1 = body.RightArm
		rightWeld.C1 = cframe(-1.2,0.5,0.4)* CFrame.Angles(math.rad(270),math.rad(-5),0)
	end
end)

tool.Unequipped:connect(function()
	if body.LeftArm and body.RightArm and body.Torso and body.LeftShoulder and body.RightShoulder then
		body.LeftShoulder.Part1 = body.LeftArm
		body.RightShoulder.Part1 = body.RightArm
		leftWeld.Parent = nil
		rightWeld.Parent = nil
	end
end)

repeat wait() until player.Character

if server and not player.Character:findFirstChild("InfoBox") then
	local infoBox = instance("TextLabel", player.Character)
	infoBox.Name = "InfoBox"
	infoBox.Size = UDim2.new(0,100,0,30)
	infoBox.Position = UDim2.new(1,-100,1,0)
	infoBox.BackgroundColor3 = Color3.new(0,0,0)
	infoBox.BackgroundTransparency = 1
	infoBox.BorderSizePixel = 0
	infoBox.Font = "Arial"
	infoBox.FontSize = "Size24"
	infoBox.TextColor = BrickColor.new("White")
	infoBox.TextStrokeColor3 = Color3.new(0,0,0)
	infoBox.TextStrokeTransparency = 0
	
	local screenGui, inProgress = instance("ScreenGui", player.PlayerGui), false
	
	local pingEvent = Instance.new("RemoteEvent", screenGui)
	pingEvent.Name = "Ping"
	
	pingEvent.OnClientEvent:connect(function(stamp)
		Spawn(function()
		local ping = math.ceil((time() - wait() + 0.01 - stamp) * 1000)
		infoBox.Position = UDim2.new(1,-100,1,0)
		infoBox.Parent = screenGui
		infoBox:TweenPosition(UDim2.new(1,-100,1,-30), "Out", "Quad", 0.5, true)
		infoBox.TextColor3 = ping < 200 and Color3.new(0,1,0) or ping <300 and Color3.new(1, 85/255,0) or Color3.new(1,0,0)
		infoBox.Text = "~ "..ping.." ms"
		end)
	end)
	
	player.Chatted:connect(function(message)
		if string.lower(message) == "ping" and not inProgress then
			inProgress = true
			for i=1,3 do
				server.Ping:FireServer(pingEvent, time())
				wait(2.05)
				infoBox.Parent = nil
			end
			inProgress = false
		end
	end)
end


If anyone could attempt to fix this it would be much appreciated.

1 comment

0

the gun script is probably in a script. try changing it to a localscript

1 answer

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