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.
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)
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.