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

See if a part is currently touching another part?

Asked by 4 years ago

Note: I know the event Touched exist, but it seems to not work this time.

I made a LocalScript that has a function that triggered whenever a player presses the letter 'R'. When it got triggered, it'll fire a RemoteEvent to the server and the ServerScript will create a Part and made it anchored, CanCollide to false and positioned to a little front of the character,

Then, I want it to see if there's a Part that touched it but it didn't work and I understand why. I just don't know how to fix or find an alternative way. I can't really phrase the problem I'm facing right now so I'm very sorry.

If you're curious, this is the inside of the LocalScript

local userinputservice = game:GetService("UserInputService")
local keypressre = game:GetService("ReplicatedStorage"):WaitForChild("KeyPress")

userinputservice.InputBegan:Connect(function(input, gameprocessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.R then
            keypressre:FireServer("R")
        end
    end
end)

This is the ServerScript one.

local keypressre = game:GetService("ReplicatedStorage"):WaitForChild("KeyPress")

keypressre.OnServerEvent:Connect(function(player, input)
    if input == "R" then
        print("Interact!")
        local touch = Instance.new("Part")

        local playervalue = Instance.new("ObjectValue",touch)
        playervalue.Name = "Player"
        playervalue.Value = player
        touch.Name = "TOUCH"
        touch.Anchored = true
        touch.CanCollide = false
        touch.Size = Vector3.new(2,4,2)
        touch.Parent = workspace
        touch.CFrame = player.Character.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0,0,-3))
        touch.Touched:Connect(function(part)
            if part:IsA("BasePart") then
                print("debugtest")
            end
        end)
        wait(.2)
        touch:Destroy()
    end
end)

Any help or advice is welcome.

0
if you want to see if a part touched it, then you gotta use the “Touched” event. This event waits for something to touch it then fires a function tied to it Grazer022 128 — 4y
0
Note: this is the only event that waits for something to touch/hit it Grazer022 128 — 4y
0
Ah. I get it now. unpurrity 4 — 4y
0
The script works fine for me? Are you reading the output window? Change wait(.2) to wait(5), because you're deleting the part too quickly, maybe? Is your "KeyPress" a remote event? Don't get it mixed with a remote function, make sure it's a remote event. Yozoh 146 — 4y
View all comments (2 more)
0
It is a RemoteEvent. unpurrity 4 — 4y
0
And yes I'm reading the output window unpurrity 4 — 4y

Answer this question