How can I use the new raycast in Roblox Studio? - roblox

I am trying to learn how to use the Raycast system in Roblox studio, I don't have enough experience in Roblox studio to understand what's going on here, or rather how I am going to change or update this line of code since this is deprecated.
local function castRay ()
local origin = firePoint.Position
local direction = (mouse.Hit.p - firePoint.Position).Unit
direction = direction * gunSettings.range
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
replicatedStorage.Replicate:FireServer(tool, origin)
local visual = Instance.new("Part")
local length = (pos - origin).Magnitude
Specifically on local hit, pos, this is now deprecated as stated in roblox studio, as well as local ray, now I did research that this kind of code can be used instead of the following two that I just mentioned
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
But the following lines such as length requires pos variable, I don't know how I can move forward thanks to this. So there is no error for now, but I am asking cause I want to implement the new raycast system in this function, and I'm kind of stuck on how to do it, Here is the full code of the function:
local function castRay ()
local origin = firePoint.Position
local direction = (mouse.Hit.p - firePoint.Position).Unit
direction = direction * gunSettings.range
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
replicatedStorage.Replicate:FireServer(tool, origin)
local visual = Instance.new("Part")
local length = (pos - origin).Magnitude
visual.Anchored = true
visual.CanCollide = false
visual.Material = Enum.Material.Neon
visual.Color = gunSettings.rayColor
visual.Size = Vector3.new(gunSettings.raySize.X, gunSettings.raySize.Y, length)
visual.CFrame = CFrame.new(origin, pos)*CFrame.new(0,0,-length/2)
visual.Parent = workspace.Effects
debris:AddItem(visual, gunSettings.debrisTime)
return hit, pos, direction, origin
end

If you look at the docs for WorldRoot:Raycast, you'll see a code sample on how to utilize the new raycast function.
The new API just packages the data a little differently. Rather than passing your ignorelist into the raycast function, you pass it into the RaycastParams object. And all of the data you get back is packaged into a RaycastResult object.
So you can adapt your code simply like this :
local function castRay()
local origin = firePoint.Position
local direction = (mouse.Hit.p - firePoint.Position).Unit * gunSettings.range
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = ignoreList
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
local hit = raycastResult.Instance
local pos = raycastResult.Position
The rest of your code should work just as before.

Related

Roblox studio How do I change a TextLabel text in script

I want to make a gui that show your coordinates in game with text labels but my script seems didn't change the text at all
players = game.Players.LocalPlayer
players.CharacterAdded:Wait()
X = math.floor(players.Character.HumanoidRootPart.Position.X)
Y = math.floor(players.Character.HumanoidRootPart.Position.Y)
Z = math.floor(players.Character.HumanoidRootPart.Position.Z)
text = script.Parent.Text
while true do
text = X..","..Y..","..Z
X = math.floor(players.Character.HumanoidRootPart.Position.X)
Y = math.floor(players.Character.HumanoidRootPart.Position.Y)
Z = math.floor(players.Character.HumanoidRootPart.Position.Z)
end
please help me
I wanted to change the text to XYZ position in string
but everytime i launch the game the text didnt change
and it still at default text in roblox ui text label
The simple answer to your question of how do I change a TextLabel text is you just set the Text property.
script.Parent.Text = "Hello World"
If you want a simple way to track the changing position of a player's character, I would recommend using the Changed signal on the Part. It will fire the connected function every time the property changes, and it allows you to avoid using an infinite while-loop.
local text = script.Parent
local player = game.Players.LocalPlayer
-- wait for the player's character to exist
if not player.Character then
player.CharacterAdded:Wait()
end
-- listen for changes to the player's position
local hrp = player.Character.HumanoidRootPart
hrp:GetPropertyChangedSignal("Position"):Connect(function()
local position = hrp.Position
local x, y, z = math.floor(position.X), math.floor(position.Y), math.floor(position.Z)
-- format the position, and display it in the TextLabel
local positionString = string.format("(%d, %d, %d)", x, y, z)
text.Text = positionString
end)
So I can see that you have made few mistakes here
Explanation:
First of all, players.CharacterAdded:Wait() doesn't always work and never works in studio. So instead of that, you should use players.Character:WaitForChild("HumanoidRootPart").
Second thing. By calling text = script.Parent.Text you were requesting the text inside of it (you were getting it as a string), so to simply fix that you have to use text = script.Parent and then when changing text you will have to use text.Text = "your text".
The next one is making your while loop infinite by using bool true. You can't make it like that or else the playar may experience lag or even script may crash. So I'd suggest to put this to something like wait(0.1).
And the last thing is that you should get X,Y,Z before putting it into the text or else it will be delayed.
And your final script should be looking like this:
players = game.Players.LocalPlayer
players.Character:WaitForChild("HumanoidRootPart")
X = math.floor(players.Character.HumanoidRootPart.Position.X)
Y = math.floor(players.Character.HumanoidRootPart.Position.Y)
Z = math.floor(players.Character.HumanoidRootPart.Position.Z)
text = script.Parent
while wait(0.1) do
X = math.floor(players.Character.HumanoidRootPart.Position.X)
Y = math.floor(players.Character.HumanoidRootPart.Position.Y)
Z = math.floor(players.Character.HumanoidRootPart.Position.Z)
text.Text = X..","..Y..","..Z
end
Keep scripting, nothing is easy without practice,
KubaDC

How do you clone a MODEL on Roblox?

I have seen videos and stuff on how to clone things in Roblox, but most of it is just "How to clone parts in Roblox!" and "How to clone OBJECTS in Roblox!" But still is only showing parts. My goal here is to make a system that when you touch a part, your character gets cloned to a position in the workspace, while a cut scene plays. This is like the system used in Mini Toon's game "Piggy" that broke records. I do not need help with any of the cut scene things though, I got that covered.
wait(10)
copy = game.Workspace.YourUsernameHere:clone()
copy.Parent = game.Workspace
copy.Name = "Test"
copy.Posistion = Vector3.new(0, 0, 0)
This is one of the things I have seen. I hope someone can help me with this.
The Archivable property must be set to true in order to clone the player's Character.
wait(10)
game.Workspace.YourUsernameHere.Archivable = true
copy = game.Workspace.YourUsernameHere:clone()
game.Workspace.YourUsernameHere.Archivable = false
copy.Parent = game.Workspace
copy.Name = "Test"
copy:MoveTo(Vector3.new(0, 0, 0))
Use Model:SetPrimaryPartCFrame()

Damage player when touching water terrain

I'm trying to decrease a player's health when they touch some water terrain in ROBLOX.
I'm not sure why this doesn't work, but it doesn't. If someone could help me out that would be neat.
while wait() do
local player = game.Workspace.LocalPlayer
local headLoc = game.Workspace.Terrain:WorldToCell(player.Character.Head.Position)
local hasAnyWater = game.Workspace.Terrain:GetWaterCell(headLoc.x, headLoc.y, headLoc.z)
if player.Character.Humanoid.Health ~= 0 then
if hasAnyWater then
player.Character.Humanoid:TakeDamage(0.2)
end
end
end
If you check your output window you'll see why:
LocalPlayer is not a valid member of Workspace
LocalPlayer is in "Players", so you should declare:
local player = game.Players.LocalPlayer
So if you change that, and have it in a LocalScript for example in the StarterPlayerScripts folder, everything works just like you want.
Solved.
local player = game:GetService("Players").LocalPlayer
while wait(0.5) do
local headLoc = game.Workspace.Terrain:WorldToCell(player.Character.LowerTorso.Position) or game.Workspace.Terrain:WorldToCell(player.Character.Torso.Position)
local hasAnyWater = game.Workspace.Terrain:GetWaterCell(headLoc.x, headLoc.y, headLoc.z)
if player.Character.Humanoid.Health ~= 0 then
if hasAnyWater then
player.Character.Humanoid:TakeDamage(8)
end
end
end

I am trying to make NPC that is randomly walking and attacks players

I am trying to make NPC that is randomly walking and attacks players. Can someone help me?
A good starting place for NPCs that walk around and attack players are zombies. I would recommend looking at the R15 Zombie with #### sounds model in the Toolbox. Once you drag it into the Workspace, there is a script in there called Script that is fairly straightforward in how it moves the zombie around to attack other humanoid characters.
Well, that is complex in itself, but if you are new to scripting, it might be best to just get the free-modeled zombie in the toolbox, and then change it's appearance through the explorer window. If you want to go the long route, it might be better to learn how to use the function :MoveTo. The Roblox Developer website provides good info on how to use these things. As for attacking people, just put a script in the arm that damages on touch. Here's something that might work, (put it in the same model as the humanoid):
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Players:GetPlayers()
local torso = nil
local dist = 10
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if temp2.Character:IsA("Model") then
temp = temp2.Character:FindFirstChild("Torso")
human = temp2.Character:FindFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
For the damage script, you can use this if you add it to one of the arms:
script.Parent.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
part.Parent.Humanoid:TakeDamage(30)
end
end)

FromPoint does not intersect it's own IPolyline5

I have a Polyline and a Point FeatureClass.
I create a point feature on the Point layer for both the FromPoint and the ToPoint of the IPolyline5 similar to below:
IFeature pointFeature1 = pointFeatureClass.CreateFeature ();
pointFeature1.Shape = polyline.FromPoint;
IFeature pointFeature2 = pointFeatureClass.CreateFeature ();
pointFeature2.Shape = polyline.ToPoint;
Later, I then run both the from point and to point geometries through a method like the below to find all the intersecting polyline features from the polyline feature class.
ISpatialFilter filter = new SpatialFilter ();
filter.Geometry = pointGeometry;
filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor cursor = lineFeatureClass.FeatureClass.Search (filter, false);
At the very least, the intersect filter should find the polyline off which I got the 2 points. The strange thing is, it works for the FromPoint, but not with the ToPoint.
Both feature classes are using the same Geographic Coordinate System and Projected Coordinate System.
I hope I am doing something stupid, but just can't figure out what.
I got it to work consistently with esriSpatialRelIntersects by just buffering the point by 0.001.
When creating new features from existing features, you should not use the direct reference, but the ShapeCopy. Try to change yout first block to:
pointFeature1.Shape = polyline.FromPoint.ShapeCopy;
pointFeature2.Shape = polyline.ToPoint.ShapeCopy;
instead of
pointFeature1.Shape = polyline.FromPoint;
use
PointFeature1.Shape = ((polyline.FromPoint as IPoint) as IFeature).ShapeCopy;
and for
pointFeature2.Shape = polyline.ToPoint;
use
PointFeature1.Shape = ((polyline.ToPoint as IPoint) as IFeature).ShapeCopy;