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

Why does the local part move for both players when using touch function?

Move = game.Workspace.MovingPlatform.Platform
Move2 = game.Workspace.MovingPlatform.Touch

Move2.Touched:Connect(function(hit)
	
for i = 1, 100, 1 do
	Move.CFrame = Move.CFrame * CFrame.new(Vector3.new(0.1,0,0))
	wait() 
	end	
	
end)

So this script is in the StarterGui and it is a local script. It should do so Player1 touches a part then another part moves only for Player1. So it should not show for Player2.

This happens: When I touch the part with Player1 then the other part moves for both Player1 and Player2. Does anyone know how to fix this? So it only shows for the player that touches it.

Thanks, Xsodar

1 answer

✓ Accepted answer

The touch event fires whenever any player touches the part, even if it's in a local script.

Though, I see what you are trying to do, to get the part to only move when the local player touches it.

To prevent the part from moving whenever any player touches it, you might want to check if the player that touched the part is the local one. If so, then do the loop.

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

Move = game.Workspace.MovingPlatform.Platform
Move2 = game.Workspace.MovingPlatform.Touch

Move2.Touched:Connect(function(hit)

	local humanoid = hit.Parent:FindFirstChild("Humanoid") -- Checking for a humanoid

	if(humanoid) then
		local character = humanoid.Parent
		local plr = players:GetPlayerFromCharacter(character) -- getting the player out of the character model

		if(plr == player) then
			-- Is the local player
			-- Keeping your code
			for i = 1, 100, 1 do
				Move.CFrame = Move.CFrame * CFrame.new(Vector3.new(0.1,0,0))
				wait() 
			end
		end
	end
end)

I would also suggest to have a look at the TweenService, it can come on pretty handy sometimes!

1 comment

1

Thank you very much, working :)

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