Trying to make a friend boost system but came across a slight issue that if their friend leaves/unadds them, the boost will stay. So how do I detect when a players friend leaves/unadds the player?
Script for detecting friends:
local remote = game.ReplicatedStorage.Remotes.Event
while task.wait(5) do
local players = game:GetService("Players")
local plr = players.LocalPlayer
for i, v in ipairs(players:GetChildren()) do
if plr:IsFriendsWith(v.UserId) then
remote.FriendBoost:FireServer(plr)
end
end
end
This works right now for the most part, all you need to do is tell if they have no friends in the server at all every time they check, if they dont have any friends then remove the boost.
This can be done by having a bool value, if any player is their friend set it to true, after the for loop check if its still false, if so remove the boost.
yeah but I want the boost to go up the more friends you have, so I need a way to detect how many friends are in the server so I can do the calculations accordingly
Instead of having a bool value just add up a number value for each friend they have, then at the end of the for loop just do your boost times the amount of friends they have, if they have no friends the value will be zero.
Also you should loop through all the players only when someone leaves or joins.
Add a 1 to a number value : D
In your code right now, when you check if the user is their friend, if they are, add 1 to a variable.
After the for loop take this variable and multiply it by the boost or whatever.
If you want me to write out code for you please read the thing that pops up when you start making a scripting support post.
I must say that if you cannot grasp this concept as much as I hate to admit it you should probably go learn more about some basic stuff cause you would appear to be lacking basic knowledge
you arent grasping my question. i have no problem detecting when they join. you obviously are lacking this basic knowledge as well since you are dodging the question entirely only telling me to add a number and not how to subtract it when the friend leaves
Thats the thing, please put the number variable in the while loop, this makes it local to the loop so you wont have to subtract it, it will go back to zero every time the loop runs.
If thats to easy for you you can always make an else statement when you check if the player isnt their friend and if it isnt it will do the else statement and then you can just subtract it there.
if I do it the way you say, its just going to add to the friends value and when they leave it will stay the same, and with the else statement it will just make negative numbers
local remote = game.ReplicatedStorage.Remotes.Event
while task.wait(5) do
local totalFriends = 0
local players = game:GetService("Players")
local plr = players.LocalPlayer
for i, v in ipairs(players:GetChildren()) do
if plr:IsFriendsWith(v.UserId) then
totalFriends += 1
end
end
-- do whatever
print(totalFriends)
end
This is all you need to do, nothing more to it.
The totalFriends value since its in a while loop is local to that time that it runs therefore it is 0 every time the loop runs. If you look at what it prints it will be zero if they have no friends and whatever depending on how many friends in the server.
If you dont believe me at the beginning of the while loop you can set the value to 0.
local remote = game.ReplicatedStorage.Remotes.Event
local totalFriends = 0
while task.wait(5) do
otalFriends = 0
local players = game:GetService("Players")
local plr = players.LocalPlayer
for i, v in ipairs(players:GetChildren()) do
if plr:IsFriendsWith(v.UserId) then
totalFriends += 1
end
end
-- do whatever
print(totalFriends)
end
Then with this totalFriends variable you can pass it along in your remote event, considering I dont know what the event does thats all I can say, but now you have the amount of friends the user has!
Ik very exciting
PS: Im going to sleep now so if you need anything I wont answer for a little bit