Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

This Touched Function Cannot Sense The Tool?

I am trying to make a game on Roblox where you have a tool (key) and when they tool touches the keyhole on a door it is supposed to fling the door open, but that is not happening.

Here is the script inside the tool:

script.Parent.Tip.Touched:Connect(function(keyhole) --Tip is a part at the top of my tool
	if keyhole.Name == "KeyHole1" then
		game.ReplicatedStorage.KeyHole1Open.Value = true
	end
end)

And here is the script inside the door:

while game.ReplicatedStorage.KeyHole1Open == true do
	local TweenService = game:GetService("TweenService")

	local Panel = workspace.Door
	local PanelRoot = Panel.PrimaryPart
	
	local PanelSwingInfo = TweenInfo.new() -- Let's use all defaults here
	
	local PanelSwingTween = TweenService:Create(PanelRoot, PanelSwingInfo, {
	    CFrame = PanelRoot.CFrame * CFrame.Angles(0, math.rad(180), 0)
	})

	PanelSwingTween:Play()
end

NOTE: I am new to scripting on Roblox so that is why it doesn't work.

I would really appreciate if someone helped me out here.

3 comments

0

Oh you forgot to put .Value just after KeyHoleOpen on the first line of code on your door, thats a mistake i do even nowadays lol

0

You can see where the code gets an error on output window, to know what is and in which line is the error.

0

There was no error.

2 answers

✓ Accepted answer

Scripts run when they're created or renabled.

Your while loop is created at the beginning of the game, sees if KeyHole1Open is true, which it's not and then continues the rest of the script. There's nothing after the while loop so the script stops running.

instead, use the KeyHole1Open's changed event to run the code whenever the keyhole1 value is changed.

local TweenService = game:GetService("TweenService")
local isKeyHoleOpen= game.ReplicatedStorage.KeyHole1Open

--runs whenever keyhole's value changed
isKeyHoleOpen.Changed:Connect(function()
	--check if isKeyHoleOpen's value is true
	if isKeyHoleOpen.Value == true then
		local Panel = workspace.Door
		local PanelRoot = Panel.PrimaryPart
		
		local PanelSwingInfo = TweenInfo.new() -- Let's use all defaults here
		
		local PanelSwingTween = TweenService:Create(PanelRoot, PanelSwingInfo, {
			CFrame = PanelRoot.CFrame * CFrame.Angles(0, math.rad(180), 0)
		})
		
		PanelSwingTween:Play()
	end
end)

2 comments

0

So your saying it was nothing got to do with the tool?

0

Thanks royaltoe anyway I will try this later.

Looks to me like you're changing a value from a LocalScript, which means it's only for the Client so any ServerScripts woudn't be able to detect a change.

Try firing a remote event instead of changing a value.

2 comments

0

This isn't a local script.

0

I mean the one in the tool, if that's what you mean too, then it should be a LocalScript.

This discussion is preserved as it was. New answers, comments, and votes are closed.