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: Problems with raycast-based gun.

Previous Thread :: Next Thread 
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
04 Sep 2015 05:31 PM
I posted this in Game Design, and I was told to go here.

I'm making a gun that uses raycast to detect hits. It's programmed to do extra damage for hits to the head, but it doesn't always detect it. In fact, when I hooked it up to a function that printed whatever part the gun hit, it detected NOTHING when it hits the head and does no damage. How do I fix this?

~Cookies need love like everything does.~
Report Abuse
LongKillKreations is not online. LongKillKreations
Joined: 14 Feb 2014
Total Posts: 4502
04 Sep 2015 05:32 PM
post code

also, are hats blocking it? are you ignoring hats when casting the ray?
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
04 Sep 2015 05:33 PM
@Long

It's definitely the hats. Is there a way to ignore hats? Sorry, I'm a novice to scripting.

Anyways, here's the code:

local tool = script.Parent
local user


tool.Equipped:connect(function(mouse)

user = tool.Parent


mouse.Button1Down:connect(function()

local ray = Ray.new(tool.AimPart.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")
if humanoid then
print(hit.Name)
if hit.Name == "Head" or hit.Parent.className == "Hat" then
humanoid:TakeDamage(100)
else
humanoid:TakeDamage(30)
end
end

-- Settings
local distance = (position - tool.AimPart.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Really blue")
rayPart.Parent = game.Workspace
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.AimPart.CFrame.p) * CFrame.new(0, 0, -distance/2)

game.Debris:AddItem(rayPart, 0.1)
end)
end)

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 09:42 AM
Bump
Report Abuse
litalela is not online. litalela
Joined: 30 Mar 2010
Total Posts: 6267
05 Sep 2015 10:05 AM
straight off the wiki

➳Lɪᴛᴀʟᴇʟᴀ ɪs ᴍʏ ɴᴀᴍᴇ, Lᴜᴀ ɪs ᴍʏ ɢᴀᴍᴇツ
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 11:42 AM
Bump
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 12:42 PM
Bump1

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 05:07 PM
Bump2

~Cookies need love like everything does.~
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
05 Sep 2015 05:13 PM
hit.Name == "Head" or hit.Name == "Handle"
Report Abuse
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
05 Sep 2015 05:16 PM
sry, I didn't read the code, am a lil in a hurry...

local hit, pos = game:FindPartOnRay(Ray, Ignore) --lets just say it's like that
if hit.Parent:IsA('Hat') then
hum = script.Parent.Parent:FindFirstChild('Humanoid')
else
hum = script.Parent:FindFirstChild('Humanoid')
--stuff
end

"My Life is going Good... but..."
Report Abuse
louiskk is not online. louiskk
Joined: 31 Dec 2008
Total Posts: 228
05 Sep 2015 05:19 PM
I'm guessing you might want to ignore the tool parts as well as the hat, in which case you could use FindPartOnRayWithIgnoreList which accepts a table instead of a single ignore value.
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 05:37 PM
@louis

So how would I set up the "Ignore" table? I looked up the function you mentioned on the Wiki, but the documentation was very sparse.

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 06:23 PM
Bump3

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 08:13 PM
Bump4

~Cookies need love like everything does.~
Report Abuse
litalela is not online. litalela
Joined: 30 Mar 2010
Total Posts: 6267
05 Sep 2015 08:13 PM
make sure the part you are shooting from is very very small

otherwise it will aim to the left

i know from experience

➳Lɪᴛᴀʟᴇʟᴀ ɪs ᴍʏ ɴᴀᴍᴇ, Lᴜᴀ ɪs ᴍʏ ɢᴀᴍᴇツ
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
05 Sep 2015 08:16 PM
@lit

Thanks for the advice, but it didn't really solve my problem.

~Cookies need love like everything does.~
Report Abuse
chris20060 is not online. chris20060
Joined: 24 Feb 2012
Total Posts: 235
05 Sep 2015 08:43 PM
@phoniex
Solved it
local tool = script.Parent
local user


tool.Equipped:connect(function(mouse)

user = tool.Parent


mouse.Button1Down:connect(function()

local ray = Ray.new(tool.AimPart.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")
if humanoid then
print(hit.Name)
if hit.Name == "Head" then
humanoid:TakeDamage(100)
elseif hit.className == "Hat" then
hit:Destroy()
else
humanoid:TakeDamage(30)
end
end

-- Settings
local distance = (position - tool.AimPart.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Really blue")
rayPart.Parent = game.Workspace
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.AimPart.CFrame.p) * CFrame.new(0, 0, -distance/2)

game.Debris:AddItem(rayPart, 0.1)
end)
end)
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
06 Sep 2015 11:40 AM
@Chris

No, that didn't solve the problem.

~Cookies need love like everything does.~
Report Abuse
chris20060 is not online. chris20060
Joined: 24 Feb 2012
Total Posts: 235
06 Sep 2015 01:05 PM
@Phoniex

I think this might be it, i made a mistake.

@phoniex
Solved it
local tool = script.Parent
local user


tool.Equipped:connect(function(mouse)

user = tool.Parent


mouse.Button1Down:connect(function()

local ray = Ray.new(tool.AimPart.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")
if humanoid then
print(hit.Name)
if hit.Name == "Head" or hit.className=="Hat" then
humanoid:TakeDamage(100)
else
humanoid:TakeDamage(30)
end
end

-- Settings
local distance = (position - tool.AimPart.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Really blue")
rayPart.Parent = game.Workspace
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.AimPart.CFrame.p) * CFrame.new(0, 0, -distance/2)

game.Debris:AddItem(rayPart, 0.1)
end)
end)
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
06 Sep 2015 02:51 PM
@chris

Still not fixed.

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
06 Sep 2015 03:34 PM
BUmp

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
07 Sep 2015 04:06 PM
BUMP

~Cookies need love like everything does.~
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
07 Sep 2015 04:54 PM
B1

~Cookies need love like everything does.~
Report Abuse
IllegallyBlind is not online. IllegallyBlind
Joined: 07 Nov 2010
Total Posts: 1691
07 Sep 2015 04:58 PM
Wait raycasting ignore lists accepts tables!!??

Why didn't anyone tell me...
Report Abuse
phoniex is not online. phoniex
Joined: 03 Feb 2008
Total Posts: 34985
07 Sep 2015 05:00 PM
@Illegally

I'm still not sure how the ignore list works. Can you explain?

~Cookies need love like everything does.~
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