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

Cloning Script| Not Working? [Edited]

Asked by 8 years ago

The goal is to clone to models containing Packages, into the player based on a players team. EDIT:I added a changed function, but I get the errer that changed is not a member?

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character ~= nil and Player.TeamColor ~= BrickColor.new("White")
local AwareModel = game.ReplicatedStorage.Aware
local SoldierModel = game.ReplicatedStorage.Soldier

function GiveParts(Model)
    for _, Object in pairs(Model:GetChildren()) do 
        Object:Clone().Parent = Player.Character
    end
end
function onChanged(TeamColor)
if Player.TeamColor == BrickColor.new("Crimson") then
    GiveParts(AwareModel)
elseif Player.TeamColor == BrickColor.new("Medium blue") then --Easier for me to have them seperate
    GiveParts(SoldierModel) 
elseif Player.TeamColor == BrickColor.new("Pearl") then --Easier for me to have them seperate
    GiveParts(SoldierModel)
end 
end
Player.TeamColor.Changed:connect(onChanged) 

Thanks!

1 answer

Log in to vote
0
Answered by
DataStore 530 Moderation Voter
8 years ago

**The below answer is given under the assumption the script in question is a LocalScript and is in the player's PlayerGui, Backpack or Character. **

  • Model instances don't have a 'Children' property.
  • You should use elseif statements instead of if statements after the first if statement.
  • This will only work when they first spawn - If that's what you want, then it's not an issue.
  • By simply moving the children of the two models, you prevent their reuse; you need to clone.
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character ~= nil and Player.TeamColor ~= BrickColor.new("White")

local AwareModel = game.ReplicatedStorage.Aware
local SoldierModel = game.ReplicatedStorage.Soldier

function GiveParts(Model)
    for _, Object in pairs(Model:GetChildren()) do 
        Object:Clone().Parent = Player.Character
    end
end

if Player.TeamColor == BrickColor.new("Crimson") then
    GiveParts(AwareModel)
elseif Player.TeamColor == BrickColor.new("Electric Blue") or Player.TeamColor == BrickColor.new("Pearl") then
    GiveParts(SoldierModel) 
end

Line 7: Created a function to give the model's contents to the player; easier than typing out the same thing for the three different TeamColors.

Line 8: Iterating through the given model, so that we're able to give the player the correct "kit"

Line 15: Both TeamColors receive the same, so we might as well just use 'or' rather than using a completely different if statement for the same thing.

0
Its not cloning into the players character, not errors tho pluginfactory 463 — 8y
0
Apologies. I overlooked something. BrickColors (TeamColor in this instance) can't be compared with a string. I've rectified this issue - If it doesn't work, then the player's TeamColor is being changed after the script is ran. If that's the case, then use of .Changed may be needed. DataStore 530 — 8y
0
Oh yeah... i did the same, My bad pluginfactory 463 — 8y
0
Still now working at all, still no errors :( pluginfactory 463 — 8y
View all comments (8 more)
0
The issue then is that the player's TeamColor is edited after this script is ran. I've edited the code again to add to the line I use 'repeat' - This edit prevents the rest of the script from running until the player's TeamColor changes from the default "White". I've tested the above and it does work. DataStore 530 — 8y
0
It isnt working for me, any idea why? pluginfactory 463 — 8y
0
Are you sure that you have the right TeamColors? Are you sure that the two models you're cloning from are in ReplicatedStorage? Sometimes LocalScripts don't throw their errors to the output - Try using it in a normal script (In STUDIO), and testing it through studio; looking at the output for any errors (such as things not existing, etc). (Remember to change back to a LocalScript afterwards). DataStore 530 — 8y
0
i get 11:29:50.652 - Workspace.Script:2: attempt to index local 'Player' (a nil value) pluginfactory 463 — 8y
0
It works :D pluginfactory 463 — 8y
0
Btw, idk if this is irrelivent is it possible to change the players character model, everytime players changes teams pluginfactory 463 — 8y
0
Yes, it is. You'd just need to delete the old stuff you added and add the new stuff - Just use the .Changed event to do something when the player's TeamColor property changes. DataStore 530 — 8y
0
edited it in but i think its wrong? pluginfactory 463 — 8y
Ad

Answer this question