Im trying to play a tween animation once a player touches a part, but when a player touches the part the tween animation plays for every player

im trying to play a tween animation once a player touches a part.

but when a player touches the part the tween animation plays for every player.

Server Script:

local repStorage = game:GetService(“ReplicatedStorage”)

local event = repStorage.RemoteEvent
local event2 = repStorage.RemoteEvents.BlackFrame

script.Parent.Touched:Connect(function(plr)
event2:FireAllClients()
wait(1)
event:FireAllClients(plr)
end)

Local Script: (it is located in starter gui)

local repStorage = game:GetService(“ReplicatedStorage”)
local tweenService = game:GetService(“TweenService”)

local player = game:GetService(“Players”).LocalPlayer

local event = game.ReplicatedStorage.RemoteEvents.BlackFrame

local blackFrame = script.Parent.BlackFrame

local tween = tweenService:Create(blackFrame,TweenInfo.new(1),{BackgroundTransparency = 0})
local tween2 = tweenService:Create(blackFrame,TweenInfo.new(1),{BackgroundTransparency = 1})

event.OnClientEvent:Connect(function()

tween:Play()

wait(3)

tween2:Play()

end)

I’m pretty sure it’s because you are doing FireAllClients instead of just FireClient

1 Like

i tried FireClient, and it returned with an error.

Did you put the player in the parameters?

let me try that, i never really used FireClient() before.

The touch event returns a part, not a player, so you are just any part that touches the script’s parent when doing this. I would recommend doing

script.Parent.Touched:Connect(function(part)
if part.Parent:WaitForChild("Humanoid") then
local plr = Game.Players:GetPlayerFromCharacter(part.Parent.Humanoid)
event2:FireClient(plr)
wait(1)
event:FireClient(plr)
end)

He wants to play the animation for every player, regardless of whether they touched it or not, so :FireAllClients was right.

Just wanting to say, use task.wait(), not wait().

This is the modified server code, it was showing an error when sending FireClient because you were not passing the player parameter, try testing this modification I made to your code, i also added a debounce to prevent the remote event from being executed multiple times when is pressed, you can modify the debounce time in this line: local DebounceTime = 2

Server:

local repStorage = game:GetService("ReplicatedStorage")

local event = repStorage.RemoteEvent
local event2 = repStorage.RemoteEvents.BlackFrame

local Debounces = {}
local DebounceTime = 2

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			if not Debounces[player] or (tick() - Debounces[player]) > DebounceTime then
				Debounces[player] = tick() 
				event2:FireClient(player)
				task.wait(1)
				event:FireClient(player)
			end
		end
	end
end)

Client:

local repStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")

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

local event = game.ReplicatedStorage.RemoteEvents.BlackFrame

local blackFrame = script.Parent.BlackFrame

local tween = tweenService:Create(blackFrame,TweenInfo.new(1),{BackgroundTransparency = 0})
local tween2 = tweenService:Create(blackFrame,TweenInfo.new(1),{BackgroundTransparency = 1})

event.OnClientEvent:Connect(function()
	tween:Play()
	task.wait(3)
	tween2:Play()
end)

Let me know if it worked for you, and if you encounter any errors, send me a screenshot of the output to identify the issue

Try this. Do this in a “Script” in the part to be touched. You don’t need the “LocalScript” anymore.

local TweenService = game:GetService("TweenService")

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.Touched:Connect(function(touched)
		wait(0.5)
		local GUI = plr.PlayerGui.ScreenGui.Frame --Frame/Gui
		local Tween = TweenService:Create(GUI, TweenInfo.new(1),{BackgroundTransparency = 0})
		local Tween2 = TweenService:Create(GUI, TweenInfo.new(1),{BackgroundTransparency = 1})
		
		Tween:Play()
		wait(3)
		Tween2:Play()
	end)
end)

It was hard for me to learn :FireClient() as well. It’s important to understand the difference between the player, and their character.
Players -

  • Are located in the Players service
  • Represent the true player in the game

Characters -

  • Are by default located in workspace
  • Represent the person the player controls.

FireClient() calls for a player object, not their character.

Interactions with 3D objects can return either one depending on what you’re doing. For example, BasePart.Touched returns the part that touched (which, if a character, can be found with TouchedPart.Parent) and a ProximityPrompt returns the actual player who triggered it.

Getting the player’s character and vice versa can be pretty essential. Use game:GetService(“Players”):GetPlayerFromCharacter(character) to get player from character. Use Player.Character (if one exists) to get the player’s character.

That being said, here is what you could do server side

local TouchPart = script.Parent --set this to the part that gets touched
local RemoteEvent = game:GetService(“ReplicatedStorage”).RemoteEvent --change this to the remote event
TouchPart.Touched:Connect(function(part) --this could be any part, so we have to check that it’s from a character
if part.Parent:FindFirstChild(“Humanoid”) then --we have to make sure that whats touching has a humanoid (which could be a character)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then --check to make sure it’s a player
RemoteEvent:FireClient(player)
end
end
end)

Sorry I wrote this on mobile. Hope this helps!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.