generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Gun Keeps shooting when unequipped. Why?

Previous Thread :: Next Thread 
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 01:41 AM
down = false
mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Up:connect(function()
down = false
end)

player = game.Players.LocalPlayer
local tool = script.Parent
local User
debounce = false
tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
down = true
debounce = true
while down do
while debounce do
wait(.1)
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local humanoid = (hit and hit.Parent) and hit.Parent:FindFirstChild("Humanoid") or nil
if humanoid then
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user and user.TeamColor ~= player.TeamColor then --// You missed a reliability issue, what if user is nil?
if player.Character.Humanoid.Health > 5 then
humanoid:TakeDamage(5)
end
end
end

local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = player.TeamColor
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)


game.Debris:AddItem(rayPart, 0.1)

end
wait(.1)
end
end)
debounce = false
end)

Sad For My Sad
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:01 PM
Bump.

Sad For My Sad
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
13 Apr 2015 09:03 PM
Well, that's because you have the Button1Down always connected to the mouse. The Equipped event returns the mouse so you can just use the mouse that is returned from there
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:07 PM
Um. k. I don't know how to fix that. Since it's true and false values and functions. So..

Sad For My Sad
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
13 Apr 2015 09:08 PM
Just get rid of that first part that says mouse = game.Players.LocalPlayer:GetMouse()
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:09 PM
Doesn't it always have to get the mouse though? Or is that wrong with this script?

Sad For My Sad
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:11 PM
Because if I remove that, it says unknown global mouse.

Sad For My Sad
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
13 Apr 2015 09:12 PM
Put your button1Up event inside of the Equipped function
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:12 PM
hellp...

Sad For My Sad
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
13 Apr 2015 09:12 PM
Make sure everything that refers to mouse is inside the Equipped event
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:14 PM
*Face-Palm* Still not working when I am not holding it down....

down = false

player = game.Players.LocalPlayer
local tool = script.Parent
local User
debounce = false
tool.Equipped:connect(function(mouse)
mouse.Button1Up:connect(function()
down = false
end)
user = tool.Parent
mouse.Button1Down:connect(function()
down = true
debounce = true
while down do
while debounce do
wait(.1)
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local humanoid = (hit and hit.Parent) and hit.Parent:FindFirstChild("Humanoid") or nil
if humanoid then
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user and user.TeamColor ~= player.TeamColor then
if player.Character.Humanoid.Health > 5 then
humanoid:TakeDamage(5)
end
end
end

local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = player.TeamColor
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)


game.Debris:AddItem(rayPart, 0.1)

end
wait(.1)
end
end)
debounce = false
end)

Sad For My Sad
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:16 PM
Ok wtf. Not working.

Sad For My Sad
Report Abuse
SurfaceLight is not online. SurfaceLight
Joined: 06 Apr 2014
Total Posts: 7574
13 Apr 2015 09:18 PM
Bump.

Sad For My Sad
Report Abuse
iiEssence is not online. iiEssence
Joined: 18 Jun 2014
Total Posts: 3467
13 Apr 2015 09:39 PM
Use the mouse from the parameter of .Equipped instead of getting the mouse from :GetMouse()

down = false


player = game.Players.LocalPlayer
local tool = script.Parent
local User
debounce = false
tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
down = true
debounce = true
while down do
while debounce do
wait(.1)
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local humanoid = (hit and hit.Parent) and hit.Parent:FindFirstChild("Humanoid") or nil
if humanoid then
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if user and user.TeamColor ~= player.TeamColor then --// You missed a reliability issue, what if user is nil?
if player.Character.Humanoid.Health > 5 then
humanoid:TakeDamage(5)
end
end
end

local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = player.TeamColor
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)


game.Debris:AddItem(rayPart, 0.1)

end
wait(.1)
end
end)
mouse.Button1Up:connect(function()
down = false
end)
debounce = false
end)
Report Abuse
Npokemonmaster2 is not online. Npokemonmaster2
Joined: 24 Oct 2012
Total Posts: 24
13 Apr 2015 09:41 PM
try

while down and tool.Equipped do
Report Abuse
Npokemonmaster2 is not online. Npokemonmaster2
Joined: 24 Oct 2012
Total Posts: 24
13 Apr 2015 09:46 PM
or maybe tool.Unequipped:connect(function() down=false end)
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image