Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with rays?

Asked by 9 years ago

I'm tried ignoring the Character with both rays and the mouse so he can't place any objects on him or near him but it won't work.Your still able to place blocks on the character why?

-- 6/10/2015  By:Zobomafoo
--Purpose:Creates a movable model using rays and mouse events

--[[Setup]]--

Player = game.Players.LocalPlayer
-------------------------------
Vairables = {
    Part = game.ServerStorage.Part,
    Mouse = Player:GetMouse()}
-------------------------------
Functions = {
    CreateRay = function(IgnorePart)
    local Ray = Ray.new(Vairables.Mouse.UnitRay.Origin,Vairables.Mouse.UnitRay.Direction*900)--Create ray from mouse and reaches 900 studs max
    local Hit,Pos = game.Workspace:FindPartOnRay(Ray,IgnorePart)--Ray,PartIgnoredByRay
    return Hit 
end}
-------------------------------
ClonedPart = Vairables.Part:Clone()
ClonedPart.Parent = game.Workspace

 --~~--~~[[Everything starts here]]~~--~~---
    Vairables.Mouse.TargetFilter = ClonedPart
     Vairables.Mouse.Move:connect(function()
         local Hit = Functions.CreateRay(Player.Character)
         if  Hit then -- if hit then do the following
         ClonedPart:MoveTo(Vairables.Mouse.Hit.p)
         end
     end)
 -----------------------------------------
     Vairables.Mouse.Button1Down:connect(function()
         local NewPart = ClonedPart:Clone()
         NewPart.Parent = game.Workspace
     end)


Edit:

Unable to cast Vector3 to CoordinateFrame

Script 'Players.Player.PlayerGui.LocalScript', Line 27

-- 6/10/2015  By:Zobomafoo
--Purpose:Creates a movable model using rays and mouse events

--[[Setup]]--

Player = game.Players.LocalPlayer
-------------------------------
Vairables = {
    Part = game.ServerStorage.Part,
    Mouse = Player:GetMouse()}
-------------------------------
ClonedPart = Vairables.Part:Clone()
ClonedPart.Parent = game.Workspace
-------------------------------
IgnoreTable = {Player.Character,ClonedPart}
-------------------------------
Functions = {
    CreateRay = function()
    local Ray = Ray.new(Vairables.Mouse.UnitRay.Origin,Vairables.Mouse.UnitRay.Direction*900)--Create ray from mouse and reaches 900 studs max
    local Hit,Pos = game.workspace:FindPartOnRayWithIgnoreList(Ray,IgnoreTable)
    return Hit,Pos
end}
 --~~--~~[[Everything starts here]]~~--~~---
     Vairables.Mouse.Move:connect(function()
         local Hit,Pos = Functions.CreateRay()
         if  Hit then -- if hit then do the following
         ClonedPart:SetPrimaryPartCFrame(Pos)
         end
     end)
 -----------------------------------------
     Vairables.Mouse.Button1Down:connect(function()
         local NewPart = ClonedPart:Clone()
         NewPart.Parent = game.Workspace
     end)

Why won't it work and how do I fix it?

1
I edited my answer again TurboFusion 1821 — 9y
1
You have to use CFrame.new() for a coordinate frame, not Vector3.new(). CFrame.new(X, Y, Z) TurboFusion 1821 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

when you call the CreateRay function, you put Character as the parameter. The problem with that is that you never defined what Character was, so the ray won't ignore anything.

Either define what Character is at the beginning or use Player.Character instead of Character.

Hope this helped!

EDIT:

You're calling the MoveTo method on what I assume is a single part. Well, the MoveTo method acts the same way that changing the position of a part does: If the new position is within a part, the part will be placed on that X and Z points, but the Y will be the lowest clear area. Basically, the part will be placed above whatever blocks are blocking it.

To fix that issue, change the CFrame of the block instead of moving the Model by using the MoveTo method, like so:

Part.CFrame = CFrame.new(Pos + Vector3.new(0, Part.Size.y / 2, 0)) --I'm adding half the part's Y Size because then the part will be above the ground rather in halfway into the ground

NOTE: Pos is the 2nd argument you get from the FindPartOnRay(Ray) method. Use that instead of Mouse.Hit.p because that way the part will be positioned exactly where the ray hits and you can ignore multiple objects using the FindPartOnRayWithIgnoreList(Ray, Table) method.

NOTE 2: The table in the FindPartOnRayWithIgnoreList() method has to be filled with the objects that you want to ignore, for example:

local IgnoreTable = {
    Player.Character;
    Part;
}

EDIT 2:

Yes, it would be best to use the SetPrimaryPartCFrame(Cf) method on the model. Just be aware that the model needs to have a primary part though. You can do that through the script, like so:

Model.PrimaryPart = Model.Part
0
It kinda fixed the problem but i'm able to put the model above the characters head,how do I fix i this? kevinnight45 550 — 9y
0
I'm moving a model with multiple parts so will I use the "SetPrimaryPartCFrame" method or do I just get all of the children? kevinnight45 550 — 9y
0
I got a error I can't put a vector3 to a coordinate Frame,how do I fix it? kevinnight45 550 — 9y
Ad

Answer this question