How to detect if touching a certain part from SSS

How can I modify this to add more if touching, since it is located inside a ServerScriptService. I have a script that uses a RemoteEvent so that when the player hits a button, it uses the RemoteEvent to update leaderstats by one for a simulator game. I have a zone that if your in it, it changes it by more than one, so how can I do it. ServerScriptService cannot store parts.

Script (It just changes by one, doesn’t detect if player is touching certain part):

local debounce = false
game.ReplicatedStorage.Remote.OnServerEvent:Connect(function(player)
if debounce == false then
	player.leaderstats.Splits.Value += 1
	debounce = true
	wait(1.4)
	debounce = false
end
end)

You can’t parent a part directly to ServerScriptService, but you can still manipulate and create or destroy them from it.

So you should still be able to detect if a player is touching the part through Touched in your server script

How would I detect that? Additionally, the part needs to be transparent and canCollide needs to be off (so that the player can’t see its there)

You would do it just like you would in any other script

-- in SSS
local newPart = Instance.new("Part")
newPart.Parent = workspace
newPart.Transparency = 1
newPart.CanCollide = false
newPart.Anchored = true
newPart.CFrame = --whatever you want here

newPart.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        -- technically not required but i like only doing the HRP
        -- mostly so players with like big wings won't trigger it sooner
        if part.Name == "HumanoidRootPart" then
           reward = reward + 1
        end
    end
end)

I’ve written a script that checks if for a humanoid, through a part in the workspace. Everything works, except the connect function (that connects the onTouched function)

Code:

local debounce = false
game.ReplicatedStorage.Remote.OnServerEvent:Connect(function(player)
if debounce == false then
		
		local function onTouched(Obj)
			local h = Obj.Parent.Parent.Workspace.Part:FindFirstChild("Humanoid")
			if h then
				player.leaderstats.Splits.Value += 1
			end
         end
		script.Parent.Parent.Workspace.Part:Connect(onTouched)
    end
end)

Error:

Change this:
script.Parent.Parent.Workspace.Part:Connect(onTouched)

It should be script.Parent.Parent.Workspace.Part.Touched:Connect(onTouched)

Isn’t that the same exact code?

Edit: nvm i see it now

No. You did Part:Connect. It should be Part.Touched:Connect.

No error this time, I also changed the Obj to script.

Code:

local debounce = false
game.ReplicatedStorage.Remote.OnServerEvent:Connect(function(player)
if debounce == false then
		
		local function onTouched(Obj)
			local h = script.Parent.Parent.Workspace.Part:FindFirstChild("Humanoid")
			if h then
				player.leaderstats.Splits.Value += 1
			end
         end
		script.Parent.Parent.Workspace.Part.Touched:Connect(onTouched)
    end
end)

ok, I’m confused as to why you are taking the long route to the part? It should simply be

workspace.Part

Although this probably isn’t the fix to the script

2 Likes

That looks correct.

Does it work as intended? What happens when the player touches the part?

Something that I think might be a problem is this line:
local h = script.Parent.Parent.Workspace.Part:FindFirstChild("Humanoid")

Change it to this:
local h = Obj.Parent:FindFirstChild("Humanoid")

That script works, but it has not yeild. It is supposed to only trigger when the button is pressed.

its because the touch triggers super fast, there needs to be a cooldown, I tried making my own, but it didnt work.