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

how do I make a hat that I made that stays on a players neck?

Asked by
zValerian 108
5 years ago
Edited 5 years ago

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?

0
is it replicating at all? MusicalDisplay 173 — 5y
0
Is it a custom or is it officially ROBLOX made? Cinema_Sin -3 — 5y
0
You're missing end tags on both scripts DinozCreates 1070 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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.

Ad
Log in to vote
0
Answered by
blazar04 281 Moderation Voter
5 years ago
Edited 5 years ago

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!

Answer this question