How to make this function local?

  1. What do you want to achieve?
    I want to achieve that the function given down below is local for every player.
  2. What is the issue?
    The function is in a local script and it handles multiple parts. Basically all what it does when a specific part is touched some other parts go visible and the touched button tweens down (side effect). I have 2 variants: 1. When a button is touched the parts appear forever; 2. When a button is touched the parts only stay for a specific time (different for every button). The function works as expected and there are no errors, but when I test it with multiplayer the function appears for everyone on the screen (when a button is touched), but I want that the function should only happen for the player who touched the part
  3. What solutions have you tried so far?
    I have tried localizing some instances (such as folders or parts) but nothing worked, even tho it is a local script…

Here is the Local Script which handles everything:

local info = TweenInfo.new(
	0.5,         
	Enum.EasingStyle.Linear,     
	Enum.EasingDirection.Out,    
	0,                    
	false,                 
	0                     
)

-------------------------------------------------------------------------------------No-Time Buttons

for i,v in pairs(game.Workspace.TweenButtons["No-TimeButtons"]:GetChildren()) do
	if v:IsA("Model") and v.Button.TweeningPart and v.VisibleParts then
		local Down = v.Button.Down
		local Up = v.Button.Up
		local Part = v.Button.TweeningPart
		local Folder = v.VisibleParts
		Part.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				if Part.Position == Up.Position then
					local TweenService = game:GetService("TweenService") 
					local PartTween = TweenService:Create(Part, info, {Position = Down.Position}) 
					PartTween:Play()
					Part.Press:Play()
					
					for _,Parts in pairs(Folder:GetDescendants()) do
						if Parts:IsA("Part") or Parts:IsA("UnionOperation") or Parts:IsA("MeshPart") then
							TweenService:Create(Parts, TweenInfo.new(1), {Transparency = 0}):Play()
							Parts.CanCollide = true
						end
					end
				end
			end
		end)
	end
end

-------------------------------------------------------------------------------------Timed Buttons

for _,x in pairs(game.Workspace.TweenButtons.TimedButtons:GetChildren()) do
	if x:IsA("Model") and x.Button.TweeningPart and x.VisibleParts and x.Timer then
		local Down = x.Button.Down
		local Up = x.Button.Up
		local Part = x.Button.TweeningPart
		local Folder = x.VisibleParts
		Part.Touched:Connect(function(hit)
			local Time = x.Timer.Value
			local Timer = Time
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				if Part.Position == Up.Position then
					local TweenService = game:GetService("TweenService") 
					local PartTween = TweenService:Create(Part, info, {Position = Down.Position}) 
					local PartTween_Back = TweenService:Create(Part, info, {Position = Up.Position}) 
					
					PartTween:Play()
					Part.Press:Play()
					for _,Parts in pairs(Folder:GetDescendants()) do
						if Parts:IsA("Part") or Parts:IsA("UnionOperation") or Parts:IsA("MeshPart") then
							Parts.Transparency = 0
							Parts.CanCollide = true
						end
					end
					
					repeat
						wait(1)
						Timer = Timer - 1
						Part.SurfaceGui.TextLabel.Text = Timer
					until Timer <= 0
					
					Part.SurfaceGui.TextLabel.Text = Time
					Timer = Time
					PartTween_Back:Play()
					for _,Parts in pairs(Folder:GetDescendants()) do
						if Parts:IsA("Part") or Parts:IsA("UnionOperation") or Parts:IsA("MeshPart") then
							Parts.Transparency = 1
							Parts.CanCollide = false
						end
					end
				end
			end
		end)
	end
end

Any help would be appreciated!

1 Like

Put the script in the StarterPlayerScripts

1 Like

Sadly this did not change anything… still same problem as before :confused:

1 Like

I don’t think I understand what you want to achieve?

1 Like

Ok so,
if you understand what the script does here is the problem: If one player touches a button in the tweenbuttons folder the expected function happens (button tweens down + parts appear). Now I tested the same thing with 2 players. ! player touched the part and the same function happened but the other player who did not touch the part also saw the function. I want that only the player who touched the part sees the function (so only the parts and the tween should be visible for this specific player).

1 Like

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

I think it is because you are getting the player in the for loop. You should just get the player from the top of the script like:

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

When getting the player in the for loop it does not only get one player but multiple players because you don’t specify what player.

1 Like

Tried that + putting it in starter player scripts, did not work either…

Remove it from starterplayerscripts and just put it in startergui

1 Like

There were some issues in your code but I rewrote the whole thing (don’t judge my passion lol)

If you’re curious as to what may have possibly broke it, the timer was incorrectly used.

local players = game:GetService("Players")
local tweenservice = game:GetService("TweenService")

local tweenbuttons = workspace:WaitForChild("TweenButtons")
local notimebuttons = tweenbuttons:WaitForChild("No-TimeButtons")
local timedbuttons = tweenbuttons:WaitForChild("TimedButtons")

local info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

local notimebuttonsdelay = 2 -- delay until buttons can be used again
local timedbuttonsdelay = 2 -- delay until buttons can be used again

for _, model in pairs(notimebuttons:GetChildren()) do
	if model:IsA("Model") then
		local button = model:FindFirstChild("Button")
		local visibleparts = model:FindFirstChild("VisibleParts")
		local tweeningpart = typeof(button) == 'Instance' and button:FindFirstChild("TweeningPart")

		if typeof(visibleparts) == 'Instance' and typeof(tweeningpart) == 'Instance' and tweeningpart:IsA("BasePart") then
			local down = button:WaitForChild("Down")
			local up = button:WaitForChild("Up")
			local debounce = false
			tweeningpart.Touched:Connect(function(hit)
				if debounce ~= true then
					debounce = true
					local character = hit.Parent
					local humanoid = typeof(character) == 'Instance' and character:FindFirstChildWhichIsA("Humanoid")
					local player
					if typeof(humanoid) == 'Instance' and humanoid:IsA("Humanoid") then
						if humanoid.Health <= 0 then
							return -- ignore dead players
						end
						player = players:GetPlayerFromCharacter(character)
					end
					if typeof(player) == 'Instance' and player:IsA("Player") and player == players.LocalPlayer then -- lol typechecking go brr
						tweenservice:Create(tweeningpart, info, {Position = down.Position}):Play()
						local press = tweeningpart:FindFirstChild("Press")
						if typeof(press) == 'Instance' and press:IsA("Sound") then
							press:Play()
						end
						for _, part in pairs(visibleparts:GetDescendants()) do
							if part:IsA("BasePart") then
								tweenservice:Create(part, TweenInfo.new(1), {Transparency = 0}):Play()
								part.CanCollide = false
							end
						end
					end
					task.wait(notimebuttonsdelay)
					debounce = false
				end
			end)
		end
	end
end

for _, model in pairs(timedbuttons:GetChildren()) do
	if model:IsA("Model") then
		local timer = model:FindFirstChild("Timer")
		local originaltime = typeof(timer) == 'Instance' and timer:IsA("IntValue") and timer.Value
		local button = model:FindFirstChild("Button")
		local visibleparts = model:FindFirstChild("VisibleParts")
		local tweeningpart = typeof(button) == 'Instance' and button:FindFirstChild("TweeningPart")

		if typeof(visibleparts) == 'Instance' and typeof(tweeningpart) == 'Instance' and tweeningpart:IsA("BasePart") then
			local down = button:WaitForChild("Down")
			local up = button:WaitForChild("Up")
			local debounce = false
			tweeningpart.Touched:Connect(function(hit)
				if debounce ~= true then
					debounce = true
					local character = hit.Parent
					local humanoid = typeof(character) == 'Instance' and character:FindFirstChildWhichIsA("Humanoid")
					local player
					if typeof(humanoid) == 'Instance' and humanoid:IsA("Humanoid") then
						if humanoid.Health <= 0 then
							return -- ignore dead players
						end
						player = players:GetPlayerFromCharacter(character)
					end
					if typeof(player) == 'Instance' and player:IsA("Player") and player == players.LocalPlayer then -- lol typechecking go brr
						tweenservice:Create(tweeningpart, info, {Position = down.Position})
						local press = tweeningpart:FindFirstChild("Press")
						if typeof(press) == 'Instance' and press:IsA("Sound") then
							press:Play()
						end
						for _, part in pairs(visibleparts:GetDescendants()) do
							if part:IsA("BasePart") then
								part.Transparency = 0
								part.CanCollide = true
							end
						end
						local surfacegui = tweeningpart:FindFirstChildWhichIsA("SurfaceGui")
						local textlabel = typeof(surfacegui) == 'Instance' and surfacegui:FindFirstChild("TextLabel") or {Text = ''}
						while timer.Value > 0 do
							task.wait(1)
							timer.Value -= 1
							textlabel.Text = timer.Value
						end
						textlabel.Text = '0'
						timer.Value = originaltime
						tweenservice:Create(tweeningpart, info, {Position = up.Position})
						for _, part in pairs(visibleparts:GetDescendants()) do
							if part:IsA("BasePart") then
								part.Transparency = 1
								part.CanCollide = false
							end
						end
					end
					task.wait(timedbuttonsdelay)
					debounce = false
				end
			end)
		end
	end
end

Oh my,
tysm for doing that!!! I tested the code and now some things are broken:

  1. There is no delay, so if you touch the Part the functions happen again, even if the function is still happening
  2. the function is still not local :frowning:
    BUT your coding looks so professional like the task.wait() or the retunr function!
1 Like

Oh yeah my bad. Just add a debounce if you don’t want that seamless touch response.

1 Like

is there a way to make this local?

I don’t see any function that is being used, though I assume you’re talking about the touched function used in the connection?

1 Like

i want it like this (I use the code for an obby and it would be bad if you touched the button after you reached it by beating some obby and another player could just skip this part because the parts are already spawned…)

I’ll assume your english isn’t too good but from what I can tell you want each checkpoint to be available to use only WHEN it’s the objective?

Checkpoint? Basically what I mean is that when a player touches the tween button, the following functions only apply to that player, other players have to touch the tween button for themselves to get the same effect :slight_smile:

Looks like the script is a server-side script, and the player is retrieved during the touch connection, so it doesn’t seem like it’ll give anyone else the same effect.

OH now I see… You want it to be individually usable. You’d use a LocalScript then.

1 Like

Check if the player is players.LocalPlayer, I’ll edit my code really quick, just wait…

1 Like

Here is a video:


as you can see even if the player touched the button it is visible for both players. And I am using a local script…

1 Like