This gun script works perfectly in studio but only half works in testing or in a server.
--[[
This gun scripted by: TheDoubleJ0Jake0Jam
Thanks for using my gun script.
Ammo and reloading are the final update plans for this script, then I will move on to bigger projects.
I created this gun as a way for me to learn how to script tools.
It was suprisingly easy.
--]]
--player variables
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
--gun variables
local gun = script.Parent
local ammo = 10
local reloading = false
local auto = false -- set to false if you do not want the gun to be automatic
local firing = false
if not game.workspace:FindFirstChild("Bullet") then
local bullet_storage = Instance.new("Folder", workspace)
bullet_storage.Name = "Bullet"
else
--nothing
end
function getBullet()
gun.Handle.Fire:Play() --when in a test server it errors here
local mh = mouse.Hit
local B = Instance.new("Part", workspace.Bullet)
B.CFrame = gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
B.CFrame = CFrame.new(B.Position, mh.p)
B.FormFactor = "Custom"
B.Size = Vector3.new(0.2, 0.2, 0.2)
B.BrickColor = BrickColor.Black()
B.CanCollide = true
local velo = Instance.new("BodyVelocity", B)
velo.velocity = B.CFrame.lookVector * 120
velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
B.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10, 25)
end
end)
end
function autofire()
while firing == true do
wait(0)
gun.Handle.Fire:Play()
local mh = mouse.Hit
local B = Instance.new("Part", workspace.Bullet)
B.CFrame = gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
B.CFrame = CFrame.new(B.Position, mh.p)
B.FormFactor = "Custom"
B.Size = Vector3.new(0.2, 0.2, 0.2)
B.BrickColor = BrickColor.Black()
B.CanCollide = true
local velo = Instance.new("BodyVelocity", B)
velo.velocity = B.CFrame.lookVector * 120
velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
B.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10, 25)
end
end)
wait(0.005)
end
end
mouse.Button1Down:connect(function()
if auto == true then
firing = true
autofire()
else
getBullet()-- and it errors here
end
end)
mouse.Button1Up:connect(function()
if firing == true then
firing = false
print(auto)
end
end)
local uis = game:GetService("UserInputService") -- allows firemode change
uis.InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.V then
if auto == true then
auto = false
else
auto = true
end
end
end)
error is " attempt to index upvalue char a nil value"
Those are the only 2 lines showing an error in output.
When in test server it places a part in the center of the baseplate. It doesn't add velocity, and the hit function is not called. The size of the bullet should be, "0.2, 0.2, 0.2". The size created in test mode is 2x4 studs. Any help would be appreciated.
Edit: Removed variable "char" gun script now works.
