TweenService does not play when target part is touched

I am trying to make a local script for when you enter a cave section in my game and you hit the trigger part, it plays a created Tween that makes the fog go dark, and ambience sounds lower.

 local Lighting = game:GetService("Lighting")
local part = game.Workspace.trigger1
local tweenService = game:GetService("TweenService")
local sound1 = game.Workspace.WindHowl
local sound2 = game.Workspace.Blizzard

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)

local goal = {
	Lighting.FogColor == Color3.fromRGB(0,0,0),
	volumePart.Volume == 0.050,
	volumePart2.Volume == 0.1
}

local tween = tweenService:Create(part, Lighting, volumePart, volumePart2, tweenInfo, goal)

part.Touched:Connect(function()
	tween:Play()
end)

This is a LocalScript inside of StarterPlayerScripts, the trigger part is Anchored and CanCollide is turned off.

I am still in the learning of lua and im starting to get into the more advanced site and this is what i tried to make for a lil’ test, so i also don’t know much of only what properties or parts can be changed by TweenService and i’ve been burning my head over why this does not work!!

1 Like

I have two things to potentially help.

  1. This is in a LocalScript. I’m pretty certain that part.Touched doesn’t work in LocalScripts. Because of this, you’d want to do something like this:
-- SERVER SCRIPT (ServerScriptService)
part.Touched:Connect(function(hit) -- detect hit
    if hit.Parent:FindFirstChild("Humanoid") then
     -- ^^ make sure the part was hit by a player, not another part
        someRemoteEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent)
        -- ^^ Fire event to client
    end
end)

-- LOCAL SCRIPT (StarterPlayerScripts)
someRemoteEvent.OnClientEvent:Connect(function()
    -- ^^ Receive client event
    -- Tween play here
end)
  1. This is less important, but I’ve never used multiple parts for a tween like that, so I’m not sure if that’s causing a problem. I won’t say it is, but it might be.

Good luck, and let me know if you have any further questions.

ALSO: I recommend trying JUST the tween without the .Touched to make sure that works as well.

1 Like

Everything goes fine until Line 12 (where the tween is created) in the Local Script, where in the output console it throws an error that states “Unable to cast to Dictionary”.

Also one of the problems indeed was that there were too many arguments being used, so i shortened it to only the fog event to be able to test it more

DarknessEvent = game.ReplicatedStorage.DarknessEvent
DarknessEvent.OnClientEvent:Connect(function()
	local Lighting = game:GetService("Lighting").FogColor
	local tweenService = game:GetService("TweenService")

	local tweenInfo = TweenInfo.new(1)

local goal = {
	Color3.fromRGB(0,0,0)
}

	local tween = tweenService:Create(Lighting, tweenInfo, goal)
	tween:Play()
	print("tween has played")
end)

I feel like this can be achieved by making multiple Tweens. You have way too many parameters and I feel like this is causing the problem.

Here are some changes:

local tween1 = tweenService:Create(part, tweenInfo, {FogColor = Color3.fromRGB(0,0,0)});
local tween2 = tweenService:Create(volumePart, tweenInfo, {Volume = 0.05});
local tween3 = tweenService:Create(volumePart2, tweenInfo, {Volume = 0.1});

local db = false; -- this is just for cooldowns, if you don't want this you can remove it

part.Touched:Connect(function()
	if db then return end; -- this too
	db = true; -- and this

	tween1:Play();
	tween2:Play();
	tween3:Play();
end)

If this doesn’t work, let me know the error in the output. I hope this helps.

In accordance with what @Ginger_Cat482 has said, you are using the “goal” argument a little bit wrong. Instead of using “Lighting.FogColor” for your first argument, you’d want to use just “Lighting” (in line 3, remove “.FogColor”). In the goal table, change it to “FogColor = Color3.fromRGB(0, 0, 0)”

Tweens are formatted like this.

TweenService:Create(Object, TweenInfo, {Property = newValue})
the Object is simply an item (for example, a part). You specify the property in the goal argument.

I hope this helps. Feel free to ask any more questions!

1 Like

For the future; you can only put 1 instance (the first argument) in a tween service create function
the goal table must be structured as {
[property] = value
}

and you can not do comparisons, its 1 equals sign to set a value.
for this you create multiple tweens with different goal tables

1 Like

I will make sure to follow more of these examples when making a tween again, thanks!

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