How can I make this script work properly? This is a script that is inside a lava part in one of my obbys which is suppose to kill someone if they touch it, Anyways I tried making it so it would kill someone specific if they were in a specific group and rank with "GetRankInGroup" function but I could not get it to work.
This is the script:
local part = script.Parent part.Touched:connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") local playername = hit.Parent.Name if humanoid then if playername:GetRankInGroup(2582242)== 255 then humanoid.Health = 100 else humanoid.Health = 0 end end end)
This is the functional script before I attempted adding "GetRankInGroup":
local part = script.Parent part.Touched:connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") local playername = hit.Parent.Name if humanoid then if playername == "262187" or playername == "" then humanoid.Health = 100 else humanoid.Health = 0 end end end)
Here's a fixed version of your script:
local part = script.Parent local groupInformation = {} game.Players.PlayerAdded:connect(function(player) groupInformation[player.UserId] = player:GetRankInGroup(2582242) end) game.Players.PlayerRemoving:connect(function(player) groupInformation[player.UserId] = nil end) part.Touched:connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if groupInformation[player.UserId] == 255 then humanoid.Health = 100 else humanoid.Health = 0 end end end)
Here's a rundown of what changed:
First off, I added this bit to the top of your script:
local groupInformation = {} game.Players.PlayerAdded:connect(function(player) pcall(function() groupInformation[player.UserId] = player:GetRankInGroup(2582242) end) end) game.Players.PlayerRemoving:connect(function(player) groupInformation[player.UserId] = nil end)
This makes it so that whenever a player joins, their rank in the specified group will be cached. It's expensive to make an HTTP request every time someone touches a brick. This way, we'll know whether they're in the group once they enter, making checking whether they're in the group mostly instantaneous.
Also note that it's always important to pcall async functions that make web requests. Something can always go wrong.
Next, I changed this:
local player = game.Players:GetPlayerFromCharacter(hit.Parent) if groupInformation[player.UserId] == 255 then
What you were doing before is trying to run methods on playername
, which won't work, since playername
is a string, not a player. What I did instead is use a method from game.Players
to fetch the player object from the character model.
Next, instead of making an HTTP request to check what rank the player is in the specified group, which would be slow, we just check our cache of player ranks in that group.
I hope this helps. Good luck!