Attempt to index nil with 'Play'

Hello! I’m trying to tween an object but on the client-side. It doesn’t work though and it errors. Can you help fix this?

Local Script in StarterGui:

local access = game.ReplicatedStorage.Events:WaitForChild("AccessGranted")

access.OnClientEvent:Connect(function(tween1, tween2)
	tween1:Play()
	wait(3)
	tween2:Play()
end)

Script it fires in:

local tweenService = game:GetService("TweenService")

local accessGranted = game.ReplicatedStorage.Events:WaitForChild("AccessGranted")

local door = workspace.Door

local openDoor = tweenService:Create(door, TweenInfo.new(1), {Position = Vector3.new(8, -0.5, 48)})
local closeDoor = tweenService:Create(door, TweenInfo.new(1), {Position = Vector3.new(8, -0.5, 56)})

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	if player.Backpack:FindFirstChild("Key") then
		accessGranted:FireClient(player, openDoor, closeDoor)
		print("Access granted!")
	else
		print("Access denied. Comeback with the key!")
	end
end)

It seems that tween1 and tween2 is nil, which I’m guessing because you can’t pass tweens through remote events. I believe you should create the tweens in the local script so that you can easily play it from there.

Updated Local Script:

local access = game.ReplicatedStorage.Events:WaitForChild("AccessGranted")

local door = workspace.Door

local openDoor = tweenService:Create(door, TweenInfo.new(1), {Position = Vector3.new(8, -0.5, 48)})
local closeDoor = tweenService:Create(door, TweenInfo.new(1), {Position = Vector3.new(8, -0.5, 56)})

access.OnClientEvent:Connect(function()
	openDoor:Play()
	wait(3)
	closeDoor:Play()
end)

Updated Server Script:

local tweenService = game:GetService("TweenService")

local accessGranted = game.ReplicatedStorage.Events:WaitForChild("AccessGranted")

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	if player.Backpack:FindFirstChild("Key") then
		accessGranted:FireClient(player)
		print("Access granted!")
	else
		print("Access denied. Comeback with the key!")
	end
end)
2 Likes

Try send door as the parameter, and Tween from the local script. Or what @BabyNinjaTime said.

1 Like

It worked perfectly. Thanks @BabyNinjaTime and @CipherFunctions.