Script works but not how I want it to work?

Hey! I made a weld script that attachs a part to a certain team. The problem is it only attaches if I either reset or load the character, I don’t want to do that. How do I make it work instantly as I get set to a team?

Heres the script:

local killerteam = game:GetService("Teams")["Killer"]

game.PlayerAdded:Connect(function(play)
    play.CharacterAdded:Connect(function(player)
        if play.TeamColor == killerteam.TeamColor then
            local character = play.Character or play.CharacterAdded:Wait()
            local kb = game.ReplicatedStorage.KillBrick:Clone()
            kb.Parent = character
            local weld = Instance.new("WeldConstraint")
            weld.Parent = character
            weld.Part0 = kb
            weld.Part1 = character.Head
            kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,2))
            kb.Orientation = Vector3.new (-90,0,0)
            kb.IsInUse.Value = true
        end
    end)
end)

Try this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

--//Variables
local KillBrick = ReplicatedStorage.KillBrick
local KillerTeam = Teams.Killer

--//Tables
local Connections = {}

--//Functions
Players.PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		if player.Team == KillerTeam then
			local character = player.Character
			
			if character then
				local killBrick = KillBrick:Clone()
				killBrick.IsInUse.Value = true
				killBrick.Parent = character

				local weld = Instance.new("WeldConstraint")
				weld.Part0 = killBrick
				weld.Part1 = character.Head
				weld.Parent = character

				killBrick.CFrame = character.Head.CFrame + Vector3.new(0, 0, 2)
				killBrick.Orientation = Vector3.new(-90, 0, 0)
			end

			Connections[player] = player.CharacterAdded:Connect(function(character)
				local killBrick = KillBrick:Clone()
				killBrick.IsInUse.Value = true
				killBrick.Parent = character

				local weld = Instance.new("WeldConstraint")
				weld.Part0 = killBrick
				weld.Part1 = character.Head
				weld.Parent = character

				killBrick.CFrame = character.Head.CFrame + Vector3.new(0, 0, 2)
				killBrick.Orientation = Vector3.new(-90, 0, 0)
			end)
		else
			local connection = Connections[player]

			if connection then
				connection:Disconnect()
			end

			--//Reset the character back to normal here
		end
	end)
end)
2 Likes

I didn’t think of that. Thank you so much

Just edited it again because I forgot to set the parent of an instance after.

No problem, if you need any more help feel free to ask.

Hey I tried this in game and when I became killer, if i move it flings me really high and if i move my screen with my mouse it moves me too

The script works but when you rotate, you pivote around your center of mass which is really far because the block is really far away and it causes you to fling. this didnt happen to the original script so I don’t know what causes it

Heres where the killbrick ends up, why does it end up there?
image

I don’t know what the brick does because I just reformatted and edited the functions a bit, I’ll try fixing it though.

1 Like

its offsetted, I realized that there might be something wrong with the cframe

Does this work? (I just used your old code for setting the part and welds):

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

--//Variables
local KillBrick = ReplicatedStorage.KillBrick
local KillerTeam = Teams.Killer

--//Tables
local Connections = {}

--//Functions
Players.PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		if player.Team == KillerTeam then
			local character = player.Character

			if character then
				local kb = KillBrick:Clone()
				kb.Parent = character

				local weld = Instance.new("WeldConstraint")
				weld.Parent = character
				weld.Part0 = kb
				weld.Part1 = character.Head

				kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,2))
				kb.Orientation = Vector3.new (-90,0,0)
				kb.IsInUse.Value = true
			end

			Connections[player] = player.CharacterAdded:Connect(function(character)
				local kb = KillBrick:Clone()
				kb.Parent = character
				
				local weld = Instance.new("WeldConstraint")
				weld.Parent = character
				weld.Part0 = kb
				weld.Part1 = character.Head
				
				kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,2))
				kb.Orientation = Vector3.new (-90,0,0)
				kb.IsInUse.Value = true
			end)
		else
			local connection = Connections[player]

			if connection then
				connection:Disconnect()
			end

			--//Reset the character back to normal here
		end
	end)
end)

It still doesn’t work I dont even know why

Hm, when you reset does it work?

Its a round game so the whole game ends and it sends you to the lobby

I don’t think so, and for the purposes of the game, your team resets when you die so I don’t think it really matters.

That’s pretty weird why it’s bugging then. Try adding a wait before setting the part and see if it works.

nevermind i got it working perfectly after i made a little change. in the script i had been welding it before i moved it to the player which is what caused the insane offset

1 Like