How do I change this script to morph all players instead of one?

Hi, so I’m trying to make a feature in my game where you use a proximity prompt to buy a gas mask for all players, and I have a script to morph the player into a model with the gas mask. Problem is, I don’t know how to apply this to all players in the lobby. If anyone can figure this out, I would appreciate it!

local model = script.Parent
local prompt = model.Prompt.ProximityPrompt

prompt.Triggered:Connect(function(player)
	local oldCharacter = player.Character
	local morphModel = model:FindFirstChildOfClass("Model")
	local newCharacter = morphModel:Clone()
	
	newCharacter.HumanoidRootPart.Anchored = false
	newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
	
	player.Character = newCharacter
	newCharacter.Parent = workspace
end)
2 Likes

This post should go in: #help-and-feedback:scripting-support

(The Code Review category is for code feedback rather than solving problems)


Just loop through all the players

local model = script.Parent
local prompt = model.Prompt.ProximityPrompt

prompt.Triggered:Connect(function()
    for _, player in game.Players:GetPlayers() do -- loop through all the players
		local oldCharacter = player.Character
		local morphModel = model:FindFirstChildOfClass("Model")
		local newCharacter = morphModel:Clone()
	
		newCharacter.HumanoidRootPart.Anchored = false
		newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
	
		player.Character = newCharacter
		newCharacter.Parent = workspace
    end
end)
2 Likes

Hello! I would like to share an updated version of your same idea but in a more modular and adaptable way:

local function applyGasMaskMorph(player, model)
    local oldCharacter = player.Character
    local morphModel = model:FindFirstChildOfClass("Model")
    local newCharacter = morphModel:Clone()

    newCharacter.HumanoidRootPart.Anchored = false
    newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)

    player.Character = newCharacter
    newCharacter.Parent = workspace
end

local model = script.Parent
local prompt = model.Prompt.ProximityPrompt

prompt.Triggered:Connect(function(player)
    for _, player in ipairs(game.Players:GetPlayers()) do
        applyGasMaskMorph(player, model)
    end
end)
1 Like

It worked! Thanks, and mb for putting the post in the wrong area

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.