What do you want to achieve?
I want to achieve that the function given down below is local for every player.
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
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
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).
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
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…)
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
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.