in this script im trying to see if the player clicked their own character, or if they clicked another players character then the remote event would fire. either case scenario that it’d fire.
when i click another player, the remote event fires, so thats working but it wont work if the player clicks their own character
script:
local event = game.ReplicatedStorage:WaitForChild("plrClickEvent")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
local model = mouse.Target:FindFirstAncestorOfClass('Model')
local char = player.Character
if model or char:FindFirstChild("Humanoid") and mouse.Target == char and player.Character.Humanoid.Health > 0 then -- trying to detect if plr clicked their own character
local clickedPlayer = game.Players:GetPlayerFromCharacter(model)
if clickedPlayer then
event:FireServer(clickedPlayer)
end
end
end)
You could try firing a raycast from the camera’s position towards Mouse.Hit and see if your character is caught in that raycast. Pretty sure Mouse.Hit filters out your own character by default, but I could be wrong?
tysm for replying. thats actually a great idea. i looked at other posts along this topic and i think ur onto smthn. a lot of ppl said to use raycasting. this is what i tried so far. not sure what im doing but i tried this. this is like my second time using raycasting so i have no idea what im doing sorry.
--local event = game.ReplicatedStorage:WaitForChild("plrClickEvent")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character
mouse.Button1Down:Connect(function()
local camPos = workspace.CurrentCamera.CFrame.Position
local mouseHit = mouse.Hit.Position
local mouseTarget = mouse.Target
local raydirection = camPos + mouseHit
local raycastResult = Ray.new(camPos, raydirection)
if raycastResult then
print("clicked!")
else
warn("no raycast result")
end
end)
First of all, in order to cast a ray, you should use workspace:Raycast(). In your example you only created a ray, which isn’t the same thing as casting it. You can’t do much with just the ray existing.
Second of all, that method requires two values - the origin point and the direction. It seems you have already done that in Ray.new(), but there is an issue with it.
The origin point is obviously camPos, so we are almost done here. The way you calculate the ray’s direction, however, is wrong.
We already both have the destination - mouseHit, and the origin - camPos, so once we calculate the direction, we can then pass the origin and the direction to workspace:Raycast(). Should a result be returned, you can check for the following properties of the raycast.
Since we are looking to find the player’s character, check whether the raycast result is a part that is a parent/descendant of the character, then kill the character.
Final thing - I don’t think you need mouse.Target for anything. It will never return parts associated with your local character, since mouse filters out your character’s parts by default. What we just did is a clever workaround over that filter.
If you need any more explanations, please let me know.
i got it thx 2 u! i had workspace:Raycast ugh! just didn’t know the difference between what i did and the :Raycast:. tysm 4 taking the time 2 type all that out. i really appreciate ur help. raycasting is lowkey rlly cool. u explained it better than anyone could.
one thing about the raycast is that when it hits an accessory/the handle of the accessory in the character it doesnt run the function but if it hits a part on the character it does.
however, when i take out if characterModel ~= mychar then return end then it works but then if i click another player it’ll kill me and i dont want that. any way to fix that?
script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mychar = script.Parent
local hum:Humanoid = mychar.Humanoid
mouse.Button1Down:Connect(function()
local camPos = workspace.CurrentCamera.CFrame.Position
local mouseHit = mouse.Hit.Position
local raydirection = mouseHit - camPos
local raycastResult = workspace:Raycast(camPos, raydirection)
if raycastResult then
print(raycastResult.Instance)
local raycastInstance = raycastResult.Instance
local characterModel = raycastResult.Instance.Parent
if characterModel ~= mychar then return end -- if the plr clicks on another player then dont run the function
if characterModel then
hum.Health = 0
end
else
warn("no raycast result")
end
end)
Oh yeah, btw, I worded it wrong when I mentioned “parents/descendants”, I meant children/descendants. You probably know what a child or descendant is.
Body parts are children of the character, yes. Accessories too, but here’s the trick - accessories have “Handles” in them, which means that the handles are a not a child of the character. They are a descendant. You see where I’m getting at?
So in that case we have to set two conditions for getting the character with a raycast - one being checking if the hit part’s parent is your character (which has already been done), the other being if the hit part’s parent is an accessory + that accessories’ parent is your character. Here’s how to set those conditions up:
local characterModel -- we keep this blank because we will write into this variable later
local raycastInstance = raycastResult.Instance
if raycastInstance:IsA("BasePart") then
if raycastInstance.Parent == mychar then
characterModel = raycastInstance.Parent -- Hierarchy: Body part -> Character
elseif raycastInstance.Parent:IsA("Accessory") and raycastInstance.Parent.Parent == mychar then -- I think it's "Accessory"? Either that or "Accoutrement", see which works because I'm too lazy to check lol
characterModel = raycastInstance.Parent.Parent -- Hierarchy: Handle -> Accessory -> Character
end
end
if characterModel then
hum.Health = 0
end
This has to work. If it doesn’t, I probably had made a typo, since I wrote this on the fly, but I’m sure you could figure it out.