Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I weld a cloned part to a player?

*I'm new so please explain the error rather than just fixing the problem. This is the Server Script which is located in the (ServerScriptService)

hitBoxEvent = game.ReplicatedStorage.RemoteEvents.HitBoxEvents.HitboxEvent

hitBoxEvent.OnServerEvent:Connect(function()
local player = game.Players.LocalPlayer
local char = player.Character
local hitboxOriginal = game.ReplicatedStorage.HitBoxes.BodyHitBox
local hitboxCopy = hitboxOriginal:Clone()
hitboxCopy.CFrame = char.Torso.CFrame * CFrame.new(0,0,0)
local weld = Instance.new("Weld")
weld.Part0 = char.Torso
weld.C0 = char.Torso.CFrame:Inverse()
weld.Part1 = hitboxCopy
weld.C1 = hitboxCopy.CFrame:Inverse()
weld.Parent = hitboxCopy
end)

This is the local script which is located in the (StarterPlayerScripts)

local plr = game.Players.LocalPlayer
local char = plr.Character
game.Players.PlayerAdded:Connect(function()
game.ReplicatedStorage.RemoteEvents.HitBoxEvents.HitboxEvent:FireServer()
end)

1 answer

✓ Accepted answer

a localplayer cant be set in a server script

hitBoxEvent.OnServerEvent:Connect(function()
local player = game.Players.LocalPlayer--cant do that
local char = player.Character

so do this instead

hitBoxEvent.OnServerEvent:Connect(function(player)--events fired from a localscript always come with a player argument FIRST, doesnt matter what you put inside FireServer() (stuff inside fireserver() are arguments that come after the player that fired the server)

local char = player.Character

the stuff in the localscript needs to be in the server script, like this:

game.Players.PlayerAdded:Connect(function(player)
local char = player.Character
local hitboxOriginal = game.ReplicatedStorage.HitBoxes.BodyHitBox
local hitboxCopy = hitboxOriginal:Clone()
hitboxCopy.CFrame = char.Torso.CFrame * CFrame.new(0,0,0)
local weld = Instance.new("Weld")
weld.Part0 = char.Torso
weld.C0 = char.Torso.CFrame:Inverse()
weld.Part1 = hitboxCopy
weld.C1 = hitboxCopy.CFrame:Inverse()
weld.Parent = hitboxCopy
end)

in other words, the local script doesnt need to exist (i guess) other than that everything else is fine (i guess) edit: hitboxcopy doesnt have a parent, so it would normally not exist anywhere. make sure to set one

1 comment

0

Thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.