Great tutorial, there has probably not been a reply in a very long time, so sorry for the interruption, but if you have the time, I want to understand this and I was wondering if you can answer me a few questions
- on the frontend script
local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
tool.Activated:Connect(function()
remote:FireServer(cursor.Hit.Position) -- this is the part I am talking about
end)
so when it says cusor.Hit.Position, when you say (custor.HIT.Position) is that what the mouse is clicking on? and then we are sending it to the backend script?
- on the backend script
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local Workspace = game:GetService("Workspace")
remote.OnServerEvent:Connect(function(player, position) -- this is what I am talking about
-- Nothing
end)
ok so you getting some variables from the frontend script and you it seems to me you are getting the position, and the player. But your frontend script dosenāt have a position and player variable, so can you explain that part to me?
- The backend script
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
remote.OnServerEvent:Connect(function(player, position)
local origin = shoot_part.Position
local direction = (position - origin).Unit -- this is the part I don't understand
local result = Workspace:Raycast(shoot_part.Position, direction*300)
end)
ok this is a short one, I understand that you are casting a ray at the shoot part or the tip of the gun and getting the position, and then firing it * 300 studs, but the direction variable, I looked at the intro to raycasting on the dev forum, and I can see you are calculating the direction, but in the end the unit part, what is that? And what is it used for?
- In the backend script
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local Workspace = game:GetService("Workspace")
remote.OnServerEvent:Connect(function(player, position)
local origin = shoot_part.Position
local direction = (position - origin).Unit
local result = Workspace:Raycast(shoot_part.Position, direction*300)
local intersection = result and result.Position or origin + direction*300 -- this is what I am talking about
end)
I am just downright confused on this part, why did you put result and result.Position? Can you just explain this part a little more?
- in the backend script
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
remote.OnServerEvent:Connect(function(player, position)
local origin = shoot_part.Position
local direction = (position - origin).Unit*300
local result = Workspace:Raycast(origin, direction)
local intersection = result and result.Position or origin + direction -- this is another part
local distance = (origin - intersection).Magnitude -- this is the part
local bullet_clone = ServerStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
end)
I donāt know if this part was already explained, but first, what is does intersection stand for? And why is it used in the distance variable?
and thatās all, the rest I completely understand
thanks for reading!