Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you test for players near you?

Asked by
yut640 91
7 years ago
Edited 7 years ago

Back at it again with the questions to help my player 'needs' scripting....

So this one is a bit more simple.

I have needs set up in a gui... Here is an example of a script I have which decreases your need to use the bathroom once you hit a block (100 = full need 0= no need)

script.Parent.Touched:connect(function(part)

    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

    if plr then

        local hunger=plr:FindFirstChild("PlayerGui").Bladder.Hunger

        hunger.Value = hunger.Value - 2

    end

end)

This works perfectly (ignore hunger its just... a thing i refuse to rename.)

Now, is there a way for this to do the same thing, however have a script running that basically does as follows

Every second it: (while wait(1) do)

  • Tests to see if another player is within a... 20 unit distance from the player

  • if that says true, it increases the value by 2 every second for the social need meaning the script would be:

local hunger=plr:FindFirstChild("PlayerGui").**Social**.Hunger

Thank you!

PS: I don't know how to mark answers as correct so please tell me how to so I can give you credit!!

Edit: Script that is close to, but not working.

-- Local Script in StarterPack or something

-- Variables
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local Torso = char:WaitForChild("Torso")
local Social = game.Players.LocalPlayer.PlayerGui.Social.Hunger-- This is the correct location.
local MaxDistance = 20 -- Distance before social goes up

-- Function
local function CheckPlayers()
    for i,player in pairs(game.Players:GetPlayers()) do
        if v ~= plr then
            local otherTorso = player.Character.Torso
            local Distance = (Torso.Position-otherTorso.Position).Magnitude
            if (Distance <= MaxDistance) then
                return true
            end
        end
    end
    return false
end

-- Main Loop
while wait(1) do
    if CheckPlayers() then
    print("True") -- output is currently only spamming this
        Social.Value = Social.Value -2 -- This has to be minus instead of plus b/c we are taking the need of social away by being closer to other players.
else
    print("False") -- never seen this in output yet.
    end
end

1
I just tested the script only to notice I put v instead of player on line 13. I edited the script, tested it, and I know it works now. Sorry about the typos. User#11440 120 — 7y
0
Yup, working now thank you :) yut640 91 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

This is actually more tricky than you think,

Expect a long answer.

  • First,

To get a distance from one object to/from another, you can use Magnitude. Example,

local part1 = workspace.Part1
local part2 = workspace.Part2

local Distance = (part1.Position-part2.Position).Magnitude
print(Distance)
Same concept, but we'll be using Character's Torsos.

  • Second,

We need to get all the players and check if the Distance is less than a certain number. I would suggest a pairs loop. A pairs loop will loop through a table getting all the values. If we want to loop through all the players, we can use :GetPlayers to return a table and use a pairs loop on that table! Example,

for i,player in pairs(game.Players:GetPlayers()) do
    print(player.Name)
end
That will print all the players' names.

  • Third

To get the Torsos of all these players, we first need to get the characters. Lucky for us, there's a neat property of the players that allows us to get the Character rather easily. After we get a player, we just need to use .Character to get the character. After that, we can get the Torso. Example,

for i,player in pairs(game.Players:GetPlayers()) do
    local Torso = player.Character.Torso
end
random text for graphic pleasings.

  • Fourth,

Use a LocalScript and a Loop to constantly check if the Local Player is near another Player. It's important to use Local Scripts when you can, and when working with PlayerGui. Local Scripts don't work in workspace so use StarterPack or something :3


  • Combining What We've Learned,

After Combining all these things, we should get a script similar to this,

-- Local Script in StarterPack or something

-- Variables
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local Torso = char:WaitForChild("Torso")
local Social = blah.blah -- Fix this
local MaxDistance = 20 -- Distance before social goes up

-- Function
local function CheckPlayers()
    for i,player in pairs(game.Players:GetPlayers()) do
        local otherTorso = player.Character.Torso
        local Distance = (Torso.Position-otherTorso.Position).Magnitude
        if (Distance <= MaxDistance) then
            return true
        end
    end
    return false
end

-- Main Loop
while wait(1) do
    if CheckPlayers() then
        Social.Value = Social.Value +2 -- Fix this :3
    end
end
Warning, this may not work. If this doesn't work, please tell me the error and I'll fix this as soon as I get the chance.

Edit,

We're currently getting our own torso, so the magnitude would be 0, which is less than MaxDistance. Let's just make sure v doesn't equal our player.

-- Local Script in StarterPack or something

-- Variables
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local Torso = char:WaitForChild("Torso")
local Social = blah.blah -- Fix this
local MaxDistance = 20 -- Distance before social goes up

-- Function
local function CheckPlayers()
    for i,player in pairs(game.Players:GetPlayers()) do
        if player ~= plr then
            local otherTorso = player.Character.Torso
            local Distance = (Torso.Position-otherTorso.Position).Magnitude
            if (Distance <= MaxDistance) then
                return true
            end
        end
    end
    return false
end

-- Main Loop
while wait(1) do
    if CheckPlayers() then
        Social.Value = Social.Value +2 -- Fix this :3
    end
end

I hope I helped you!

Good Luck!

How to accept answers,

Look under my name to the left and you should see an accept answer button. Here's a link to an image of what this should look like.

If you don't see this, try logging out and logging back in. If you've changed roblox usernames try updating your SH account through the settings. If you still can't figure it out, click the feedback and Ask a question or report the bug.

If I did help, please don't forget to accept my answer.
0
Thank you for the lengthy detail! The script is not working currently, I added prints into the main loop to show in output if it was true or false and so far the output is only putting true (even when a player is alone in the server.) I'll keep playing around with the script, hopefully you can work up a quick fix. Thank you so much for your time it is very appreciated. yut640 91 — 7y
1
Ah, I know why. It's getting it's own toros xD Easy fix. One Second User#11440 120 — 7y
1
Edited. Please don't forget to accept love <3 User#11440 120 — 7y
0
Currently the output is still spamming true, I will post an edit to the original post with the script and changes I made, hopefully we can figure this out :) yut640 91 — 7y
Ad

Answer this question