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

How do I weld a cloned part to a player?

Asked by 4 years ago
Edited 4 years ago

*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

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
4 years ago
Edited 4 years ago

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

0
Thank you Kehachi 2 — 4y
Ad

Answer this question