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

How can I give a position on a ray to the server?

Asked by 6 years ago

I was able to create a bullet trail on my client, but I want other players to be able to see it. To do this I am creating the part on the server, as you see below.

local function trace()
        -- Create
        local trace = Instance.new("Part")
        trace.Anchored = true
        trace.CanCollide = false
        trace.Transparency = 0.5
        trace.BrickColor = BrickColor.new("White")
        trace.Material = Enum.Material.SmoothPlastic

        -- Calculate
        local distance = (hole.CFrame.p - position).magnitude
        trace.Size = Vector3.new(0, 0, distance)
        trace.CFrame = CFrame.new(hole.CFrame.p, position) *CFrame.new (0, 0, -distance/2)
        trace.Parent = workspace

        -- Clean-up
        game:GetService("Debris"):AddItem(trace, 0.1)

However, I am not sure how I can get the position on the ray from the localscript below to the server so I can use it for creation of the bullet trail.

-- Cast Ray
        local ray = Ray.new(hole.CFrame.p, (mouse.hit.p - hole.CFrame.p).unit * range.Value)
        local touch, position = workspace:FindPartOnRay(ray, player.Character, false, true)

2 answers

Log in to vote
1
Answered by 6 years ago

Hello.

You can send all the data to the server using RemoteEvents. RemoteEvents provide the ability to send data from the client to the server in an efficient manner.

-- Server
local function trace(player,position)
    -- Code here
end
remoteEvent.OnServerEvent:Connect(trace) -- define remoteEvent as a RemoteEvent placed in workspace or replicated storage.
-- Client
local ray = Ray.new(hole.CFrame.p, (mouse.hit.p - hole.CFrame.p).unit * range.Value)
local touch, position = workspace:FindPartOnRay(ray, player.Character, false, true)
remoteEvent:FireServer(position) -- same here

The arguments provided in the trace function is the local player that came from the FireServer, as well as the position received from the client.

Please note that there will be a tiny delay in this code. That's called latency, which is the delay of sending information from a client to the server, and this can change at different times, but it's usually never more than half a second.

Lastly, as incapaz said, you did make a variable named trace after naming the function trace. Make sure that everything is named differently so the script doesn't get confused as to what is what.

Hope this helps, make sure to accept my answer if it helped. Have a good day! :)

0
Thank you so much! SpookyBo1 -1 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago

This is on lines 1 and 3. You named your function trace, but then made a variable named trace. Rename your function or your variable.

0
This won't solve what he's asking. Please use a comment for short notes like this. Thundermaker300 554 — 6y
0
This would solve it tho. User#19524 175 — 6y

Answer this question