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

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

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.

1 comment

0

Don't think a local script will work inside a part, it has to be in a player..

1 answer

✓ Accepted answer

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)

3 comments

2

This script is overly complicated. Terrible

0

Thanks this is perfect! If it works answer will be accepted!

0

@cabbler Yet no one else posts a solution.

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