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

Why is my LocalScript showing for all players when run?

I am coding an obby game, but I encountered an issue. My shrinking platform is shrinking for all players when touched which is an issue when in a large server. I'm confused as all of the code is inside of a LocalScript inside of the StarterPlayerScripts. How could I change my code or use a different method to get the effect to only show up on the client side?

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")

for _, ShrinkingPlatform in pairs(CollectionService:GetTagged("ShrinkingPlatform")) do
	
	local debounce = true
	local OriginalSize = ShrinkingPlatform.Size
	local ShrinkTime = ShrinkingPlatform.ShrinkTime.Value
	local RespawnTime = ShrinkingPlatform.RespawnTime.Value

	ShrinkingPlatform.Touched:Connect(function(hit)
		if debounce ~= false and hit.Anchored ~= true then

			debounce = false

			local TweeningInfo = TweenInfo.new(ShrinkTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
			TweenService:Create(ShrinkingPlatform, TweeningInfo, {Size = Vector3.new(0, 0, 0)}):Play()

			wait(RespawnTime + ShrinkTime)

			ShrinkingPlatform.Size = OriginalSize

			debounce = true


		end
	end)
end

Feel free to ask any questions!

1 answer

✓ Accepted answer

Since you are detecting when the part is hit, each client script for each client is looking to see when its touched and when touched by literally anyone it will trigger it, all you need to do is add a check of if its the local player that hit it.

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local locplayer = game.Players.LocalPlayer

for _, ShrinkingPlatform in pairs(CollectionService:GetTagged("ShrinkingPlatform")) do

	local debounce = true
	local OriginalSize = ShrinkingPlatform.Size
	local ShrinkTime = ShrinkingPlatform.ShrinkTime.Value
	local RespawnTime = ShrinkingPlatform.RespawnTime.Value

	ShrinkingPlatform.Touched:Connect(function(hit)
		local partsofplayer = hit:FindFirstAncestor(locplayer.Name)

		if partsofplayer then
			if debounce ~= false and hit.Anchored ~= true then

				debounce = false

				local TweeningInfo = TweenInfo.new(ShrinkTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
				TweenService:Create(ShrinkingPlatform, TweeningInfo, {Size = Vector3.new(0, 0, 0)}):Play()

				wait(RespawnTime + ShrinkTime)

				ShrinkingPlatform.Size = OriginalSize

				debounce = true


			end
		end
	end)
end

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