I dunno how to use remote events

I’m trying to send out a ray from a little foward from a player’s HumandRootPart every time a tool is activated.

But I have a problem where when I make the ray and it hits a humanoid I can’t decrease their Health. I’m not sure if this is because I have to :fireallclients, or I’m not using remote events correctly, or I’m not referencing the humanoid correctly.

Also I’m not sure how to use remote event’s parameters

There’s not a lot of solutions, and I think my problem might be is that I don’t use :fireallclients or something, but also I’m not sure how to reference the Instance(Humanoid) from the server script. Also I’m pretty sure my problem isn’t what is happening for most things I find on the Dev Hub.

Also I’d appreciate any help with optimization and more obvious stuff.

local script:

local tool = script.Parent
local repStore = game:GetService("ReplicatedStorage")
local rayEvent = repStore.Events.rayEvent
local char = game:GetService("Players").LocalPlayer.Character
local tools = game:GetService("Players").LocalPlayer.Backpack


tool.Activated:Connect(function()
	local aO = workspace.aO
	local bO = workspace.bO
	local A = aO:Clone()
	local B = bO:Clone()
	local params = RaycastParams.new()
	A.CFrame = char:WaitForChild("HumanoidRootPart").CFrame + Vector3.new(0,0,-1)
	B.CFrame = A.CFrame + Vector3.new(0,0,-10)
	params.FilterDescendantsInstances = {tools}
	params.FilterType = Enum.RaycastFilterType.Exclude
	A.Transparency = 1
	B.Transparency = 1
	A.CanCollide = false
	B.CanCollide = false
	A.Parent = workspace
	B.Parent = workspace
	
	local Direction = B.Position - A.Position
	
	local raycastResult = workspace:Raycast(A.Position, Direction, params)
	
	if raycastResult and raycastResult.Instance then
		print(raycastResult.Instance)
		if raycastResult.Instance.Parent ~= script.Parent.Parent and raycastResult.Instance.Parent.Parent == char then
			if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
				rayEvent:FireServer(raycastResult.Instance.Parent:FindFirstChild("Humanoid"))
				A:Destroy()
				B:Destroy()
			end
			
		else do
			print("Instance.", raycastResult.Instance, raycastResult.Instance.Parent)
			end
			
		end
	end
end)

Server script:

game.ReplicatedStorage.Events.rayEvent.OnServerEvent:Connect(function(player, raycastResult)
	raycastResult.Instance.Parent:FindFirstChild("Humanoid").Health -= 10
end)
game.ReplicatedStorage.Events.rayEvent.OnServerEvent:Connect(function(player, raycastResult)
	raycastResult.Health -= 10
end)

you already sent the humanoid through the client

Oh yeah a thing I forgot to add. How do I make the origin and direction not be based off of the parts I used for origin and direction, and instead based entirely off of vectors?

It no work. :frowning_face: . I used the character inserter from moon animator to test. I’m not sure if this is a problem since it does have a humanoid, or do I need to test this in the actual game and not in studio?

Here, try this:

CIient

local tool = script.Parent
local repStore = game:GetService("ReplicatedStorage")
local rayEvent = repStore.Events.rayEvent
local char = game:GetService("Players").LocalPlayer.Character
local tools = game:GetService("Players").LocalPlayer.Backpack


tool.Activated:Connect(function()
	local aO = workspace.aO
	local bO = workspace.bO
	local A = aO:Clone()
	local B = bO:Clone()
	local params = RaycastParams.new()
	A.CFrame = char:WaitForChild("HumanoidRootPart").CFrame + Vector3.new(0,0,-1)
	B.CFrame = A.CFrame + Vector3.new(0,0,-10)
	params.FilterDescendantsInstances = {tools}
	params.FilterType = Enum.RaycastFilterType.Exclude
	A.Transparency = 1
	B.Transparency = 1
	A.CanCollide = false
	B.CanCollide = false
	A.Parent = workspace
	B.Parent = workspace
	
	local Direction = B.Position - A.Position
	
	local raycastResult = workspace:Raycast(A.Position, Direction, params)
	
	if raycastResult and raycastResult.Instance then
		print(raycastResult.Instance)
		if raycastResult.Instance.Parent ~= script.Parent.Parent and raycastResult.Instance.Parent.Parent == char then
			if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
				rayEvent:FireServer(raycastResult.Instance)
				A:Destroy()
				B:Destroy()
			end
			
		else do
			print("Instance.", raycastResult.Instance, raycastResult.Instance.Parent)
			end
			
		end
	end
end)

Server:

game.ReplicatedStorage.Events.rayEvent.OnServerEvent:Connect(function(player, raycastResult)
	raycastResult.Parent:FindFirstChildWhichIsA("Humanoid").Health-=5135773493.79461347087591358705941
end)

It didn’t work :frowning_face: . I don’t know what I’m doing wrong to be honest.

Also, what’s Animator.EvalutionThrottled? It keeps saying it’s not on.

Also, If I don’t respond for about a day after this. I’m probably at school.