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

How do I find the part I touched in a server side script?

Asked by 4 years ago

I have a localscript which fires a remoteevent when certain blocks are touched but I need to be able to know the specific block I touched on the server. Is this possible?

the localscript

local character = script.Parent 
local humanoid = character:WaitForChild("Humanoid")
local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("touchedEvent")

local debounce = false
local touched = humanoid.Touched:Connect(function(hit, limb)
    if debounce then
        return
    end

    -- if part not in object folder, return
    if hit.Parent ~= workspace.Objects then
        return
    end

    remote:FireServer() -- fires the server script when the object is touched

    debounce = true
    wait(0.2)
    debounce = false
end)

the server script (idk how to find the block I touched)

local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("touchedEvent")

remote.OnServerEvent:Connect(function(player)

    local character = player.Character
    local humanoid = character:WaitForChild("Humanoid")

    local headScale = humanoid:WaitForChild("HeadScale")
    local dScale = humanoid:WaitForChild("BodyDepthScale")
    local wScale = humanoid:WaitForChild("BodyWidthScale")
    local hScale = humanoid:WaitForChild("BodyHeightScale")


end)

1 answer

Log in to vote
1
Answered by 4 years ago

Just pass the part as an argument to your remote's :FireServer() method.

In local script, line 17:

remote:FireServer(hit) -- sends the touched part

In server script, line 4:

remote.OnServerEvent:Connect(function(player, part) -- the part touched gets stored in 'part'

From here you can reference the touched part from inside the server script by addressing the 'part' variable.

Ad

Answer this question