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

Raycast works properly in studio but not when running in a server, how to fix?

Asked by 2 years ago
local RayEvent = Instance.new("RemoteEvent")
RayEvent.Name = "RayEvent"
RayEvent.Parent = script.Parent

RayEvent.OnServerEvent:Connect(function()
    local part = script.Parent
    local orgin = part.Position
    local direction = part.CFrame.LookVector * 50
    local ray = Ray.new(orgin, direction)
    local raycastResult = workspace:Raycast(ray.Origin, ray.Direction)
    print(raycastResult)
    local midpoint = orgin + direction/2
    local part = Instance.new("Part")
    part.Parent = game.Workspace
    part.Anchored = true
    part.CanCollide = false
    part.Material = Enum.Material.Neon
    part.BrickColor = BrickColor.new("Bright yellow")
    part.CFrame = CFrame.new(midpoint, orgin)
    part.Size = Vector3.new(0.02, 0.02, direction.magnitude)
    wait(0.1)
    local hitpart = raycastResult.Instance
    local human = hitpart:FindFirstChild("Humanoid")
    if (human ~= nil) then
        human.Health = human.Health - 5
end

end)

I'm trying to cast out a ray from a part that upon contact deals damage, but when in server it doesn't deal damage. What's weird to me that the code to make the bullet trail works, but the function to print and deal damage doesn't, I'm unsure of how to fix this.

I'm using a local script to fire this event, that detects when the player clicks. I'm doing this instead of a tool cause the way I have set up my custom character the weapon is attached to their hand directly in the model rather than being a tool.

0
Would it be possible to share the bit where you are calling this event from your local script? TGazza 1336 — 2y
0
 local RayEvent = script.Parent:WaitForChild("RayEvent") local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() Mouse.Button1Down:Connect(function() RayEvent:FireServer() end) OmegaNoobBones 4 — 2y

Answer this question