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

[SOLVED] Roblox "Clone" function seems to not work, is it glitched?

Asked by 4 years ago
Edited 2 years ago

Explanation: So I'm trying to write a simple code but the code doesn't seem to work. So I have 3 teams in the game named "Hospital Staff", "Patient" and "Guest". The player will be in team "Hospital Staff" only if they are in the group and have certain rank. They will be "Patient" if they are in the group and are only a member of it. Player will be in team "Guest" if they are not in the group.

Problem: I tried to clone a tool named "KeyCard" from ServerStorage to Player's Backpack but it seemed to be glitched. I tried to print "Cloned" if the tool is cloned in the Backpack and "Not Cloned" if it is not. What surprises me is that when I joined the game, the script printed "Cloned" but I did not have any tool in my Backpack. Then I tried to check my Backpack in explorer but could not find as well.

Here is my ServerScript located in ServerScriptService named "TeamAssignServer" :

local Players = game:GetService("Players")
local Teams = game:GetService("Teams");

local Hospital_Staff = Teams["Hospital Staff"]
local Patient = Teams.Patient
local Guest = Teams.Guest

Players.PlayerAdded:Connect(function(Player)
    if(Player:IsInGroup(5786709)) then
        print("Player Is In Group: Robloxia City Hospital")
        print("Player's Rank Is: "..Player:GetRankInGroup(5786709))
        local Rank  = Player:GetRankInGroup(5786709)
        if Rank > 1 then
            Player.TeamColor =  Hospital_Staff.TeamColor
            print("Team Changed, Player's Team Is Hospital Staff")
            local KeyCardClone = game.ServerStorage.KeyCard:Clone()
            KeyCardClone.Parent = Player.Backpack
            if Player.Backpack:FindFirstChild("KeyCard") then
                print("KeyCard Cloned")
            else
                print("KeyCard Not Cloned!")
            end
        elseif (Rank == 1) then
            Player.TeamColor = Patient
            print("Team Changed, Player's Team Is Patient")
        end
    else
        Player.TeamColor = Guest
        print("Player Is Not In Group: Robloxia City Hospital!")
        print("Team Changed, Player's Team Is Guest")
    end
end)

1 answer

Log in to vote
2
Answered by
Nanomatics 1160 Moderation Voter
4 years ago

There is actually nothing wrong with your script, it's just you are doing everything very quickly.

That being said the player didn't fully load in when you cloned the tool into their backpack, that's why you didn't see it. So I added a line that waits until the character loads in.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams");

local Hospital_Staff = Teams["Hospital Staff"]
local Patient = Teams.Patient
local Guest = Teams.Guest

Players.PlayerAdded:Connect(function(Player)
    repeat wait() until Player.Character
    if(Player:IsInGroup(3426179)) then
        print("Player Is In Group: Robloxia City Hospital")
        print("Player's Rank Is: "..Player:GetRankInGroup(3426179))
        local Rank  = Player:GetRankInGroup(3426179)
        if Rank > 1 then
            Player.TeamColor =  Hospital_Staff.TeamColor
            print("Team Changed, Player's Team Is Hospital Staff")
            local KeyCardClone = game.ServerStorage.KeyCard:Clone()
            KeyCardClone.Parent = Player.Backpack
            if Player.Backpack:FindFirstChild("KeyCard") then
                print("KeyCard Cloned")
            else
                print("KeyCard Not Cloned!")
            end
        elseif (Rank == 1) then
            Player.TeamColor = Patient.TeamColor
            print("Team Changed, Player's Team Is Patient")
        end
    else
        Player.TeamColor = Guest.TeamColor
        print("Player Is Not In Group: Robloxia City Hospital!")
        print("Team Changed, Player's Team Is Guest")
    end
end)

I also fixed where you assigned the player's team to Guest and Patient, you forgot the .TeamColor

Hope I helped, please let me know if you have any questions

0
Thanks. I actually tried that but i put repeat wait() until Player instead of Player.Character. Thanks for the fix too tho, didn't notice it guest_20I8 266 — 4y
Ad

Answer this question