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

Can this script be fixed using a local script inside a part?

Asked by 7 years ago

I have put this Local script into a part and it doesn't work, there are no errors in the output. Please fix the script;

-- From Local Script:

--// Services
local UIS = game:GetService("UserInputService")
local part = script.Parent
local touched = false

part.Touched:connect(function(plr)
    if touched == false then
        touched = true
    end
end)

UIS.InputBegan:Connect(function(input, gpe)
    if(input.KeyCode==Enum.KeyCode.Q) and touched == true then
        -- The rest of the code.
        print("Q was pressed.")
    end
end)

What's the problem? How come it doesn't work? This is a local script inside a part.

0
Don't think a local script will work inside a part, it has to be in a player.. donnybrook 20 — 7y

1 answer

Log in to vote
-1
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

You can't run a LocalScript from inside a brick like that. You could do something like below, but it'll need to be in a LocalScript in a place where it can run, like StarterCharacterScripts.

This script keeps a tag in your player while you're touching your brick. If the tag is there and you press Q, the function will run. If you wanted something else, you should explain further~

local player = game.Players.LocalPlayer
local char = player.Character 
local input = game:GetService('UserInputService')
local part = Workspace:WaitForChild('Part') -- the part

local debounce = Instance.new("StringValue")
debounce.Name = "Touching"

local function isDebounce()
    return player:findFirstChild("Touching")
end

local function isPlayer(hit)
    -- Checks if whatever hit our brick is still there
    if hit and hit.Parent then 
        -- Grabs the player from hit.Parent, if there is one.
        local playerHit = game.Players:GetPlayerFromCharacter(hit.Parent)
        -- if a player hits our brick and its you, then return true.
        if playerHit and playerHit == player then 
            return true
        end
    end
    -- return false if any of the if statements fail
    return false
end

part.Touched:connect(function(hit)
    if isPlayer(hit) then 
        local isDebounce = isDebounce()
        if not isDebounce then 
            debounce:Clone().Parent = player
        end
    end
end)

part.TouchEnded:connect(function(hit)
    if isPlayer(hit) then 
        local isDebounce = isDebounce()
        if isDebounce then 
            isDebounce:Destroy()
        end
    end
end)

do
    local old = isDebounce()
    if old then old:Destroy() end
end

input.InputBegan:connect(function(InputType)
    if InputType.UserInputType ==  Enum.UserInputType.Keyboard then 
        local code = InputType.KeyCode
        -- if you're pressing Q and we're touching the brick
        if code == Enum.KeyCode.Q and isDebounce() then
            print("Do the thing")
        end
    end
end)
2
This script is overly complicated. Terrible cabbler 1942 — 7y
0
Thanks this is perfect! If it works answer will be accepted! BlackOrange3343 2676 — 7y
0
@cabbler Yet no one else posts a solution. Azarth 3141 — 7y
Ad

Answer this question