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

I'm trying to make a IsInGroup script for practice. Any ideas as to why it wont or any solutions?

Asked by 5 years ago
Edited 5 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.

So, I'm trying to experiment with IsInGroup, GetRankInGroup, and GetRoleInGroup. But for some reason I can't seem to get any of them to work. I looked at tutorials and tried doing it myself, but still nothing. As shown below on line 4, the IsInGroup shows when a colon is added. So any explanation as to why this won't work?

local Player = game.Players.LocalPlayer

function Touched(plr)
    if Player:IsInGroup(3713839) then
        print("Works!")
    end
end

script.Parent.Touched:Connect(Touched)
0
need: error message. i see your problem but you tell us too User#24403 69 — 5y
0
23:11:06.118 - Workspace.Part.Script:4: attempt to index upvalue 'Player' (a nil value) iiBuilder_Boy 27 — 5y
0
You're using the Touched event wrong. Look at what the parameter is on the wiki. Also, Player is nil because you can't reference a local player in a server-script. Local player only exist locally. pidgey 548 — 5y
0
The touched event, I've used before and it worked. I put the code in the server script in a local script, but nothing, iiBuilder_Boy 27 — 5y
View all comments (3 more)
1
Oof nevermind, the touched event doesn't work. iiBuilder_Boy 27 — 5y
0
I would like to recommend you use game:GetService("Players") instead of game.Player.LocalPlayer. I can't remember of they're different things. superawesome113 112 — 5y
0
How would I make the function work? Even when I change the function to script.Parent.Touched:Connect(function(), it doesn't work. iiBuilder_Boy 27 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The issue with your script is that you cannot get the player using game.Players.LocalPlayer in a ServerScript and you cannot use the Touched Function in a local script. ROBLOX came up with a solution how you can get the player on a touched event using GetPlayerFromCharacter:

https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter

It essentially returns the player from the character.

You can put the ServerScript in a part.

-- function that runs when the part is touched.
script.Parent.Touched:Connect(function(hit)
-- variable to get the player from hit.Parent.
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
--Checks if the player is in group and prints work.
if player:IsInGroup(3713839) then
    print("Works!")
end

end)
0
Thank you for your answer. It works. All I got to do now is add in some debounce. iiBuilder_Boy 27 — 5y
0
So now how would I put this so if you click a TextButton? iiBuilder_Boy 27 — 5y
0
Nevermind. iiBuilder_Boy 27 — 5y
0
Nevermind, I still need help with my second question, iiBuilder_Boy 27 — 5y
Ad

Answer this question