Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

5 comments

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.

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

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

0

gimme a sec, im gonna try to translate this into a remote event for you

0

thank you so much

1 answer

✓ Accepted answer

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

3 comments

0

OnServerInvoke is a callback member of RemoteFunction; you can only set the callback value, get is not available

0

replace PathToRemoteFunction.OnServerInvoke:Connect(function (player, pos, dir, s) to PathToRemoteFunction.OnServerInvoke = function (player, pos, dir, s)

0

thank you so much! I don't know how you did it but you did it

This discussion is preserved as it was. New answers, comments, and votes are closed.