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

Script makes all players grow. how can i fix it so it just does the player who pressed the button?

I'm making a game where if you click on the text button, it would set you to a specific size. it does work, but all players on the server are made that size, rather than just the player. after looking through the code, I found out that this line of code is the reason why it does everyone.

for i, player in pairs(game.Players:GetPlayers()) do
			PlayerSize(player)

how do I fix this so that way the player who clicks that button is set to that size?

EDIT: it might be easier to help if i put the entire script.

local Enabled = false



script.Parent.MouseButton1Click:Connect(function()


	if not Enabled then
		Enabled = true
		function PlayerSize(player)
			local SizeValue = 25

			local humanoid = player.Character.Humanoid
			if humanoid then
				if humanoid:FindFirstChild("BodyHeightScale") then
					humanoid.BodyHeightScale.Value = SizeValue
				end
				if humanoid:FindFirstChild("BodyWidthScale") then
					humanoid.BodyWidthScale.Value = SizeValue
				end
				if humanoid:FindFirstChild("BodyDepthScale") then
					humanoid.BodyDepthScale.Value = SizeValue
				end
				if humanoid:FindFirstChild("HeadScale") then
					humanoid.HeadScale.Value = SizeValue
				end
			end
		end

		wait(2)
		for i, player in pairs(game.Players:GetPlayers()) do
			PlayerSize(player)
		end

	else
		Enabled = false
		function PlayerSize(player)
			local SizeValue = 25

			local humanoid = player.Character.Humanoid
			if humanoid then
				if humanoid:FindFirstChild("BodyHeightScale") then
					humanoid.BodyHeightScale.Value = SizeValue
				end
				if humanoid:FindFirstChild("BodyWidthScale") then
					humanoid.BodyWidthScale.Value = SizeValue
				end
				if humanoid:FindFirstChild("BodyDepthScale") then
					humanoid.BodyDepthScale.Value = SizeValue
				end
				if humanoid:FindFirstChild("HeadScale") then
					humanoid.HeadScale.Value = SizeValue
				end
			end
		end

		wait(2)
		for i, player in pairs(game.Players:GetPlayers()) do
			PlayerSize(player)
		end

	end
end)


3 comments

0

ClickDetector's pass the Player who interreacted with the Instance to your Callback as an Argument, use it as a Parameter instead of Iterating over every Player in-game.

0

Is this in a local script?

0

You should use LocalScript to get the player instead.

1 answer

The main issue with this code is in the for loop like you said, the for loop runs through every single player and changes their size. To fix this, use the local player. I am assuming that you are doing this in the PlayerGui so you are probably using a local script.

Here is the revised code:

local players = game:GetService("Players)
local player = players.LocalPlayer

repeat wait() until player.Character ~= nil --This is completely optional, its just that Roblox is weird and loads the character weirdly.

local char = player.Character or player.CharacterAdded:Wait()

script.Parent.MouseButton1Click:Connect(function()
	--now that you have the character and player reference, you can update your code accordingly. 
end)

The code above is untested, so I am unsure if it works yet.

(not related to this specific problem) Also, some pointers , in your playersize function, I would recommend to put that outside of your mouse click function. This is because you want to be able to reference the function anywhere inside your script. Also, you wont have to write it twice, as I see currently in your script.

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