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

Can you help me with my gun script? It doesn't shoot after click.

Yo, i'm back after 6 months and I came across a problem with my gun script, every time I shoot at the enemy it doesn't work, here's the part of the code that i think has that problem.

Server Script UPDATED

local event = game.ReplicatedStorage.ShootGun
local damage = math.random(20,60)
local range = 50 
local gunshot = script.Parent.gunshot

function createBeam(pos)
	print(8)
	local part = Instance.new("Part", workspace)
	part.Position = pos
	print(9)
	local at1 = Instance.new("Attachment", part)
	local at2 = Instance.new("Attachment", script.Parent.Handle)
	print(10)
	local beam = Instance.new("Beam", script.Parent.Handle)
	beam.Texture = "http://www.roblox.com/asset/?id=1274378728"
	print(11)
end


event.OnServerEvent:Connect(function(player, mousePos, originPos)
	print(1)
	local direction = (mousePos - originPos).Unit * range  
	print(2)
	local result = workspace:Raycast(originPos, mousePos)
	if result then
		print(3)
		print(4)
		local character = result.Instance.Parent 
		local humanoid = character:FindFirstChild("Humanoid")
		print(5)
		if humanoid then 
			print(6)
			humanoid:TakeDamage(damage)
		end
		print(7)
		createBeam(result.Instance.Position)
		gunshot:Play()
	end
end)

Local Script

local tool = script.Parent 
local origin = tool:WaitForChild("Handle").Position

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse()

local event = game.ReplicatedStorage.ShootGun


local recoil = false 
local recoilTime = 0.4
tool.Activated:Connect(function()
	if recoil then return end 
	recoil = true 

	local mousePosition = mouse.Target.Position

	event:FireServer(mousePosition, origin)

	wait(recoil)
	recoil = false 
end)

6 comments

0

Does it give you any errors?

0

No, except for when i shoot at the sky, then it gives me an error and then i cant use the tool anymore.

0

No, except for when i shoot at the sky, then it gives me an error and then i cant use the tool anymore. Also the if result then statement doesn't work, it skips over that.

0

show me the new code with the if check

0

local event = game.ReplicatedStorage.ShootGun local damage = math.random(20,60) local range = 50 local gunshot = script.Parent.gunshot function createBeam(pos) print(8) local part = Instance.new("Part", workspace) part.Position = pos print(9) local at1 = Instance.new("Attachment", part) local at2 = Instance.new("Attachment", script.Parent.Handle) print(10) local beam = Instance.new("Beam

0

look down

1 answer

And What's the Error

7 comments

0

it's Players.FalexForojanJunior.Backpack.Pistol.GunStart:16: attempt to index nil with 'Position'

0

its becaus eif you shoot at sky it cant get position of sky so check if target is existing

0

k, also there is another problem, when i shoot through a wall or when i shoot a player, it doesn't do anything

0

try on line 24 change direction to mouseposition

0

the if statement that's checking if target exists doesn't work, i tried shooting at the walls and dummies multiple times

0

and where is the if statement ???

0

look at the updated server script

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