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

MouseButton1Down is not a valid member of player?

So, I am very very new to creating guns.

I am testing my boundaries on the basic knowledge I know, and I seem to have messed up along the Clicking function.

local plr = game.Players.LocalPlayer
local part = Instance.new('Part')


plr.MouseButton1Down:connect(function(onclick)  
	local mouse = plr:GetMouse()
	local point = mouse.p
part.Shape = 'Ball'
part.Size = {.15, .15, .15}

part.Parent = point

part.Touched:connect(function(hit)
	if hit then
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(10)
		end
	end
end)
end)

Any and all help is appreciated.

1 answer

✓ Accepted answer

There are a few things wrong with your code. One is that by saying plr.MouseButton1Down, you're looking for an object called "MouseButton1Down" in your player. MouseButton1Down is for GUI objects, like text-buttons. You're supposed to be using the Button1Down event on your mouse. Research raycasting later.


local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:wait()
-- assuming you're using r6, else use handle if you have one.
local right_arm = character:findFirstChild("Right Arm")
if not right_arm then print("Can't find arm") return end
local mouse = plr:GetMouse()

-- Make the projectile once, clone it later.
local projectile = Instance.new('Part') 
projectile.Shape = 'Ball'
-- Use Vector3.new() Don't use curly braces.
projectile.Size = Vector3.new(1, 1, 1)

-- This is so your bullet doesn't drop
local body_force = Instance.new("BodyForce", projectile)
-- Get projectile's weight and multiply it by ROBLOX's gravity
body_force.force = Vector3.new(0,projectile:GetMass() * 196.2,0)

-- How long until the projectile gets destroyed after it was shot
local lifetime = 20 

-- Use 'C'onnect, 'c'onnect is deprecated
mouse.Button1Down:Connect(function(onclick) 
    local mouse = plr:GetMouse()
	-- mouse.Hit.p = Vector
	-- mouse.Hit = CFrame
    local point = mouse.Hit.p
    local this_projectile = projectile:Clone()
	
	-- Positions the projectile in front of your body a bit
    local spawnPosition = (right_arm.CFrame * CFrame.new(0, 0, -2)).p
	-- Points/Rotates the projectile to match your click location
	this_projectile.CFrame = CFrame.new(spawnPosition, point) 
	-- Shoots the projectile 
	this_projectile.Velocity = this_projectile.CFrame.lookVector * 200 
	-- Parent it to Workspace so it can be touched
	this_projectile.Parent = workspace
	game.Debris:AddItem(this_projectile, lifetime)
	this_con = this_projectile.Touched:Connect(function(hit)
		this_con:disconnect()
		if hit then
			-- GetPlayerFromCharacter to make sure they are a player.
			local player_hit = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player_hit and player_hit ~= plr then 
				local hum = player_hit.Character:FindFirstChild("Humanoid")
				if hum then
					hum:TakeDamage(10)
				end
				-- Destroy this_projectile on player hit, so it can still bounce off things
				if this_projectile then this_projectile:Destroy() end
			end
		end
	end)
end)

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