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

Why won't my raycast do damage in game but in studio?

Asked by 5 years ago
Edited 5 years ago

My game isn't FE enabled because I still don't know how that's done but my weapon isn't a tool it's in the camera similar to Phantom Forces and no I'm not trying to copy it I just want to make a COD style game but this is the only problem I have.

function createRay()

            local muzzle = gun[gun.Name].Flash
    local ray = Ray.new(mouse.Hit.p,(mouse.hit.p-char.Head.Position).unit*s.range)
    local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char})

    if hit then
            local human = hit.Parent:FindFirstChild("Humanoid")
            if human then
            if human.Health >0 then
            player.PlayerGui.HUD.Crosshair.Hit:Play()
            player.PlayerGui.HUD.Crosshair.Frame.Hitmarker.Visible = true

            if hit.Name == "Head" or hit.Parent:IsA("Hat") then
            human = human.Health - s.dmg * s.multi

            else

            human = human.Health - s.dmg


            end
            end
            end
            end
end

The reason I'm not using server scripts is because they can't access the camera so that's why or maybe it can I don't know but it won't work please if someone can explain it to me or tell me what I'm doing wrong that'll be a big help.

0
Forgot to add I'm not good at raycasting or really scripting in general I'm alright I do better an animations so yea there's that. GameBoyOtaku 63 — 5y
0
FE is no longer an option, it is always enabled whether the button is checked or not. Lucky for you most of your script is okay to do locally up to the human part, I would make a script do the rest via a remote event https://www.robloxdev.com/articles/Remote-Functions-and-Events Vulkarin 581 — 5y
0
But how can I do that when my weapon is in the camera? I'm not good with stuff like that... I'm lost GameBoyOtaku 63 — 5y
0
gimme a sec, im gonna try to translate this into a remote event for you misiunicholas 50 — 5y
0
thank you so much GameBoyOtaku 63 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I'm not too sure if this works, after rereading it a couple of times it seems to make sense.

If there's any errors please tell me on what line it is on. If you have any questions, i'll do my best to answer them.

Script where the gun is

function createRay()
    local muzzle = gun[gun.Name].Flash
    local fire = PathToRemoteFunction:InvokeServer(mouse.Hit.p,(mouse.hit.p-char.Head.Position).unit*s.range, s)
    if fire == "hit" then
        player.PlayerGui.HUD.Crosshair.Hit:Play()
        player.PlayerGui.HUD.Crosshair.Frame.Hitmarker.Visible = true
    end
end

Script that links to the remote

PathToRemoteFunction.OnServerInvoke:Connect(function (player, pos, dir, s)
    local function FindParts(obj) --used to find the baseparts of an object
        local list = {}
        for _,v in ipairs(obj:GetChildren()) do
            if v:IsA("BasePart") then
                table.insert(list, v)
            end
        end
        return list
    end
    local ray = Ray.new(pos,dir)
    local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,FindParts(player.Character))
    if hit then
        local human = hit.Parent:FindFirstChild("Humanoid")
        if human then
            if human.Health > 0 then
                if hit.Name == "Head" or hit.Parent:IsA("Hat") or hit.Parent:IsA("Accessory") then --included accessory since roblox has made them kinda replace hats
                    human = human.Health - s.dmg * s.multi
                    return "hit"
                else
                    human = human.Health - s.dmg
                    return "hit"
                end
            end
        end
    end
end)

This in theory should replicate the remote onto the server and the damage

0
OnServerInvoke is a callback member of RemoteFunction; you can only set the callback value, get is not available GameBoyOtaku 63 — 5y
0
replace PathToRemoteFunction.OnServerInvoke:Connect(function (player, pos, dir, s) to PathToRemoteFunction.OnServerInvoke = function (player, pos, dir, s) misiunicholas 50 — 5y
0
thank you so much! I don't know how you did it but you did it GameBoyOtaku 63 — 5y
Ad

Answer this question