Raycast not detecting players

This is a server sided script that is supposed to kill players when they are standing above my baseplate

local ray = Ray.new(workspace.BuildBase.Position,Vector3.new(0,1,0)*500)
local hit, pos = workspace:FindPartOnRay(ray, workspace.BuildBase)

print(hit)
if hit then
    local char = hit:FindFirstAncestorOfClass("Model")
    local plr = game.Players:GetPlayerFromCharacter(char)
    if plr then
        char.Humanoid:BreakJoints()
    end
end

It is not working, no errors

Do you mean like a large baseplate, that kills anyone above any part of it? Because that code will only kill a player standing above the dead center of the baseplate.

2 Likes

I think he wants a region over the baseplate to be deadly. If someone walks over the entire surface, regardless height, will kill them.

You’ll have to raycast down from each player’s position instead, with a whitelist of {workspace.BuildBase}. If the base is not rotated you could also just check if the player is within the coordinates of the base.

4 Likes

It’s for a minigame, when the round ends, kill all players that are in the barrier.

Why not just keep track of players who are in the round? Don’t check locations to decide whether or not they are.

2 Likes

This would be a better idea because it uses less resources

If it’s meant to clear players after a minigame, just track players who aren’t in the lobby by putting them in a table. When the game ends, run through that table and break the joints of their characters.

Why break the joints of their characters when you can just call LoadCharacter on their player?

There are a few ways you can resolve this, and this will kind of act as a summary of what people have said above me:

  1. Don’t rely on location of the player at all:
    Keeping track of whether or not a player is in a minigame can be handled with code. If your game teleports players into an arena, you may also want to make a table of all players who you teleported and when the minigame is over you can call something like :LoadCharacter() to respawn the character back in the lobby
  2. If you feel like raycasting is necessary, don’t raycast from the baseplate, as that will only project a ray from the center of the baseplate upwards, which isn’t what you want. Instead, raycast from each player downwards, which will return the baseplate if it is below the player.
  3. If you don’t want the players to touch the baseplate at all, you could use .Touched (the same way you would if a player stepped on lava) and just kill them that way.

I would definitely use option one because it is much more controllable than the other two.

1 Like

Because last time I used LoadCharacter, my game started lagging horribly. A friend of mine also said that LC is poorly done on the backend.

Ok, thank you. I think the easiest alternative is for me to respawn everyone.