Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

This Touched Function Cannot Sense The Tool?

Asked by 4 years ago

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.

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 Dfzoz 489 — 4y
0
You can see where the code gets an error on output window, to know what is and in which line is the error. Dfzoz 489 — 4y
0
There was no error. PrismaticFruits 842 — 4y

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

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)
0
So your saying it was nothing got to do with the tool? PrismaticFruits 842 — 4y
0
Thanks royaltoe anyway I will try this later. PrismaticFruits 842 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago

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.

0
This isn't a local script. PrismaticFruits 842 — 4y
0
I mean the one in the tool, if that's what you mean too, then it should be a LocalScript. kkkeelan999 92 — 4y

Answer this question