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)
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. . 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?
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)