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
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)
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
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
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
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
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!