I need to make a buff that stays on a players neck whenever they are on a team. Here is the script I have:
LOCALSCRIPT:
buff = game.ServerStorage.buff:Clone() if game.Players.LocalPlayer.TeamColor == BrickColor.new("Really black") then name = game.Players.LocalPlayer.Name game.ReplicatedStorage.buff:FireServer(name)
SERVERSCRIPT:
buff = game.ServerStorage.buff:Clone() game.ReplicatedStorage.buff.OnServerEvent:Connect(function(player, name) buff.Parent = game.Players:FindFirstChild(name).Character
This doesn't work at all. The buff is a model with 3 meshes. I made it myself. What do I do?
First off, you can optimize your code in the following manner:
LocalScript:
if game.Players.LocalPlayer.TeamColor == BrickColor.new("Really black") then game.ReplicatedStorage.buff:FireServer() end
Server-side Script:
buff = game.ServerStorage.buff:Clone() game.ReplicatedStorage.buff.OnServerEvent:Connect(function(player) buff.Parent = player.Character end)
It is not necessary to clone the "buff" model within the LocalScript, as it is never used (at least not in the given portion of the script.)
It would be much more effective to remove the 'name' argument altogether and do "buff.Parent = player.Character" on line 6 of the serverscript as 'player' is a reference to the Player object associated with the client that invoked the event. If you need the player's name in the future, simply use "player.Name"
The above code will still not "work" as you wish it to, because the model will only be parented to the character. In order to get it to position itself correctly and stay on the character's neck, you'll need to weld the model (Hint: Use the PrimaryPart) to the character's Torso or Head, depending on the desired effect. However, this forum is not for making scripts for people, so I'll just leave you with that.
Hopefully this will point you in the right direction. Best of luck.
If the hat is custom made, in order for the hat to stay on the neck, you have to weld the hat to the character's neck. See Weld Joints on the wiki. With your script as it is, the hat parents to the character model, but it is not connected to the character's body in any way. Welds will attach the hat to the body part you specify. Also, you need to optimize your script. It is redundant to clone the buff on both the local and server script. The OnServerEvent
event already returns the player's name, so you do not need to send the server the player's name.
LocalScript:
if game.Players.LocalPlayer.TeamColor == BrickColor.new("Really black") then game.ReplicatedStorage.buff:FireServer() end
Server script:
buff = game.ServerStorage.buff:Clone() game.ReplicatedStorage.buff.OnServerEvent:Connect(function(player) buff.Parent = player.Character end)
Good luck!