Hello!
I am trying to write a code that would only allow a ProximityPrompt to work/show if the user is in a group and at a certain minimum and maximum rank level parameter.
When I use the below code, the door does not work at all. Can someone please review this and guide me to what I have done wrong?
I am very much new to scripting and have tried looking for help here, Youtube, and Google but I could not find anything specific to this issue that would help me with this.
local GroupService = game:GetService("GroupService")
local TweenService = game:GetService("TweenService")
local hinge = script.Parent.DoorFrame.Hinge
local prompt = script.Parent.Base.ProximityPrompt
local goalOpen = {}
goalOpen.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(-75), 0)
local goalClose = {}
goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)
local tweenInfo = TweenInfo.new(1)
local tweenOpen = TweenService:Create(hinge, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(hinge, tweenInfo, goalClose)
local groupId = -- GROUP ID HERE
local minRank = -- MIN RANK HERE
local maxRank = -- MAX RANK HERE
prompt.Triggered:Connect(function()
local player = game.Players:GetPlayerFromCharacter(prompt.Parent)
if player then
local inGroup = GroupService:IsInGroup(player.UserId, groupId)
local rank = GroupService:GetRankInGroup(player.UserId, groupId)
if inGroup and rank >= minRank and rank <= maxRank then
if prompt.ActionText == "Close" then
tweenClose:Play()
prompt.ActionText = "Open"
else
tweenOpen:Play()
prompt.ActionText = "Close"
end
end
end
end)
Thank you in advance!
This would not refer to the player who activated the prompt if prompt.Parent
is not the player’s Character model.
Fortunately, when the ProximityPrompt.Triggered
event is fired and connected to a function, the player who activated the ProximityPrompt can be referenced through the first parameter. This means that local player = game.Players:GetPlayerFromCharacter(prompt.Parent)
could be removed and then the beginning of the function would be updated to the following:
prompt.Triggered:Connect(function(player)
-- Continue with the rest of the function
Oh, in addition to my previous post, I didn’t notice until now that you’re using GroupService
. That service is used to check information about a group but cannot be used to check if a player is part of a group / what their rank is.
In order to check for those things, “GroupService” would need to be replaced with a reference to the player who activated the ProximityPrompt. That means that that section of code would be updated to the following:
local inGroup = player:IsInGroup(groupId)
local rank = player:GetRankInGroup(groupId)
Correction (February 13th, 2024): Fixed the revised code to omit player.UserId
, since it’s only necessary to input the ID of the group that you want to check if the player is a member of / what rank they are in the group.
Additional Resources
Player:IsInGroup()
Method Documentation - Roblox Creator Hub
Player:GetRankInGroup()
Method Documentation - Roblox Creator Hub
Players:GetPlayerFromCharacter()
Method Documentation - Roblox Creator Hub
1 Like
I appreciate you! Though it seems that it still is not working.
I changed the script to a LocalScript and then I removed Line 1 regarding the GroupService. I updated the other lines you mentioned as well:
Updated Code
local TweenService = game:GetService("TweenService")
local hinge = script.Parent.DoorFrame.Hinge
local prompt = script.Parent.Base.ProximityPrompt
local goalOpen = {}
goalOpen.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(-75), 0)
local goalClose = {}
goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)
local tweenInfo = TweenInfo.new(1)
local tweenOpen = TweenService:Create(hinge, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(hinge, tweenInfo, goalClose)
local groupId = -- GROUP ID
local minRank = -- MIN RANK
local maxRank = -- MAX RANK
prompt.Triggered:Connect(function(player)
if player then
local inGroup = player:IsInGroup(player.UserId, groupId)
local rank = player:GetRankInGroup(player.UserId, groupId)
if inGroup and rank >= minRank and rank <= maxRank then
if prompt.ActionText == "Close" then
tweenClose:Play()
prompt.ActionText = "Open"
else
tweenOpen:Play()
prompt.ActionText = "Close"
end
end
end
end)
Is it possible you could give me feedback on if I am still missing something?
Whoops, when I referenced the code from your original post to update it, I didn’t realize that it still says player.UserId
.
For both the :IsInGroup()
and GetRankInGroup()
methods, it’s only supposed to include the ID of the group that you want to check if the player is either a member of or what their rank is, like so:
local inGroup = player:IsInGroup(groupId)
local rank = player:GetRankInGroup(groupId)
Sorry about that! I’ll update my previous post with a correction so it doesn’t confuse anyone else in the future.
As far as the most updated version of the code that you sent, one more revision that could be made would be to remove the if player then
conditional statement and the second to last end
at the bottom of the script, as it’s not necessary to check if the ProximityPrompt.Triggered
event actually returned the player object (since it should be guaranteed).
After both of those changes, here’s what the function would look like:
prompt.Triggered:Connect(function(player)
local inGroup = player:IsInGroup(groupId)
local rank = player:GetRankInGroup(groupId)
if inGroup and rank >= minRank and rank <= maxRank then
if prompt.ActionText == "Close" then
tweenClose:Play()
prompt.ActionText = "Open"
else
tweenOpen:Play()
prompt.ActionText = "Close"
end -- Ends the 'if prompt.ActionText == "Close" then' statement
end -- Ends the 'if inGroup and rank>= minRank and rank <= maxRank then' statement
end) -- Ends the function run by the 'prompt.Triggered' event