Help needed on potential cartoony game

Ok so for the past 4 hours I have been developing a game, but I have a little problem, I need to make it so that when a player gets closer than 6 studs of another player they die. I have tried on my own to accomplish this but I simply have failed every time. Please help me achieve this

2 Likes

Hmmm…

So do you want both players to die or just the player who moved towards the other player? If it’s the latter, that’d be pretty complicated. Otherwise…

Script in the Workspace.

local Players = game:GetService("Players")
local MIN_DIST = 6 --Your value can be changed if you like

local function checkPlayers()
    local getPlayers = Players:GetChildren()
    for _, player in pairs(getPlayers) do
        for _, otherPlayer in pairs(getPlayers) do
            if player ~= otherPlayer then
                if (player.Position - otherPlayer.Position).Magnitude <= MIN_DIST then
                    player.Humanoid.Health = 0
                    otherPlayer.Humanoid.Health = 0
                    return true --Means people died
                end
            end
        end
    end
    return false --Means nobody died
end

while wait() do
    local checkingTime = true
    repeat
        checkingTime = checkPlayers()
        wait()
    until checkingTime == false
    checkingTime = true
end

Haven’t tested yet. Let me know if it works.

3 Likes

kk gimme a minute I’m studying the code, did you really think that I would just copy and paste it lol

1 Like

Lol, it’s just an example. Sometimes I find it easier to explain with the actual code than words.

1 Like

can you please elaborate on how this works @Physicism Ik I’m really dumb

1 Like

Sure.

First we get the Players service. The variable MIN_DIST is just there for easy accessability. This way, in case you think 6 studs is too small or too big, you can change it easily here.

The function checkPlayers() will get a list of all the current players. I’ve got two nested for loops, each one to focus on one player. I have the if player ~= otherplayer then line there because without it, everyone would die over and over. There’s probably a way to do it with something a little more clean than two nested for loops but, like I said, it’s just an example of a way you could accomplish this.

The line if (player.Position - otherPlayer.Position).Magnitude <= MIN_DIST then is all about getting the distance between two players. However, I made a mistake here. It should’ve been if (player.Character.HumanoidRootPart.Position - otherPlayer.Character.HumanoidRootPart.Position).Magnitude <= MIN_DIST then. Models obviously don’t have positions, that’s my bad. Anyway, if the line checks out, it sets the health of both players to 0 and they die.

The while loop directly under all that will run that function every .03 seconds. If it works, please hit me with that Solution checkmark :relieved:

2 Likes

Hold on lemme test it real quicc

1 Like

The script for some reason doesn’t seem to work is it because I used the emulator???

1 Like

Can you copy and paste exactly what the error in the Output says? I’m just not quite sure what you mean by an emulator.

1 Like

that’s the weird thing there is no error and an the emulator is where 2 player dummies are entered into the game

1 Like

Bout 99.99 percent sure this will work now. Just paste this into a script in the workspace.
My mistake was not waiting for the players’ characters to appear.

local Players = game:GetService("Players")
local MIN_DIST = 6 --Your value can be changed if you like

local function checkPlayers()
	local getPlayers = Players:GetPlayers()
	for _, player in pairs(getPlayers) do
		for _, otherPlayer in pairs(getPlayers) do
			local p1 = player.Character or player.CharacterAdded:Wait()
			local p2 = otherPlayer.Character or otherPlayer.CharacterAdded:Wait()
			if p1.Name ~= p2.Name then
				if (p1.HumanoidRootPart.Position - p2.HumanoidRootPart.Position).Magnitude <= MIN_DIST then
					p1.Humanoid.Health = 0
					p2.Humanoid.Health = 0
					return true --Means people died
				end
			end
		end
	end
	return false --Means nobody died
end

while wait() do
	local checkingTime = true
	repeat
		checkingTime = checkPlayers()
		wait()
	until checkingTime == false
	checkingTime = true
end