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

How would I check if a ray on the client side hit from the serverside using remote events?

Asked by 4 years ago

So for my gun I Made my raycasts in the client side because I heard it reduces lag greatly

heres my local script

local Remote = game.ReplicatedStorage.RemoteEvent

local Tool = script.Parent
local Handle = Tool.Handle

local Player = game:GetService("Players").LocalPlayer
Tool.Equipped:Connect(function(Mouse)

    Mouse.Button1Down:Connect(function()
        local ray = Ray.new(Handle.CFrame.Position,(Mouse.Hit.Position - Handle.CFrame.Position).Unit*800)
        local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true)
        local Humanoid = part.Parent:FindFirstChild("Humanoid")

        if Humanoid then --If Humanoid was hit

Remote:FireServer()

        end
    end)

end)


But heres where I dont understand

the server script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)

-- Check if it really hit

end)

How would I go about checking if it hit or not?

1 answer

Log in to vote
0
Answered by
Nowaha 459 Moderation Voter
4 years ago
Edited 4 years ago

If you handle all the raycasting on the server side that will solve your issue. It will prevent abusing.

Local script:

local Remote = game.ReplicatedStorage.RemoteEvent

local Tool = script.Parent
local Handle = Tool.Handle

local Player = game:GetService("Players").LocalPlayer

Tool.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        Remote:FireServer(Mouse, Handle) -- The player variable is automatically passed.
    end)
end)

Server script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Mouse, Handle)
    local ray = Ray.new(Handle.CFrame.Position,(Mouse.Hit.Position - Handle.CFrame.Position).Unit * 800)
    local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true)
    local Humanoid = part.Parent:FindFirstChild("Humanoid")

    if Humanoid then --If Humanoid was hit
        -- Handle hit code here now.
    end
end)
0
You should not have any issues with lag. Nowaha 459 — 4y
Ad

Answer this question