#firing events when a part is touched

82 messages · Page 1 of 1 (latest)

tribal burrow
#

basically, I have an event called "FindExitEvent" and I want it to be fired when a part is touched, because when it gets fired, it chsbgds the text of a dialogue gui. I've managed to make it fire whenever a proximity prompt is used, but can't seem to make it whenever a part is touched

#

this works

warm vault
#
Part.Touched:connect(function(Hit)
  if Hit.Parent:FindFirstChild("Humanoid") then
      Events.FindExitEvent:FireAllClients()
end
end
tribal burrow
warm vault
#

Altought you would need to add a check to check if its a player. otherwise if anything touches it it will fire the even

warm vault
tribal burrow
warm vault
#

Added some checking to check who touched it has humanoid in it.

#

You can make it more safer if you REALLY want to make sure its a player touching it

tribal burrow
#

alr

#

ty

#

I'll lyk if it doesn't work

warm vault
#

alr

tribal burrow
warm vault
#

That works

tribal burrow
#

well

#

I touched the part and it didnt do anything

#

there wasn't any errors either

warm vault
#

try debugging it by printing

tribal burrow
#

like else print ("fail")

#

?

warm vault
#

like this for example

Part.Touched:connect(function(Hit)
print("1")
  if Hit.Parent:FindFirstChild("Humanoid") then
print("2")
      Events.FindExitEvent:FireAllClients()
end
end
#

To check at what part it doesnt print

tribal burrow
#

k

#

okay I thinks its working now

#

lemme check one more time

#

ok yes its working!! thanks alot!!

muted mesa
#

@tribal burrow mark your thread as solved

tribal burrow
#

done

warm vault
tribal burrow
warm vault
#

Oh yeah no problem then

tribal burrow
#

yea

#

wait

#

@warm vault

#

I realised

#

I can walk over the part and fire it multiple times, how can I make it only work once?

warm vault
#

add a debounce

#

if you want a cooldown

#

or if you want a toggle, for ever (like one time use) then add a toggle with datastore

tribal burrow
#

lol I'm relatively new to scripting so sorry for the hassle and stuff

warm vault
#

Its okay

#
local DataStoreService = game:GetService("DataStoreService")
local ExampleToggleName = DataStoreService:GetDataStore("ExampleToggleName") -- Change the name to whatever you want

Part.Touched:Connect(function(Hit)
    local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local success, toggleValue = pcall(function()
            return ExampleToggleName:GetAsync(Player.UserId)
        end)

        if success and not toggleValue then
            ExampleToggleName:SetAsync(Player.UserId, true)
            Events.FindExitEvent:FireAllClients()
        end
    end
end)

tribal burrow
warm vault
#

yes replace it.

tribal burrow
#

alrighty

warm vault
#

There are minor adjustments such as the Part but i think you already know how to fix it?

#

local Part = script.Parent

#

but you already know that

tribal burrow
#

yeyye

mellow prawnBOT
#

studio** You are now Level 5! **studio

tribal burrow
#

I was typing that lol

#

there we go everything's defined

warm vault
#

why is there a data store in there

warm vault
tribal burrow
warm vault
#

Right i forgot to set the toggle to true

tribal burrow
#

oh lol

warm vault
#

alr i edited it

tribal burrow
#

okey

warm vault
#

here

#

wait

#
local DataStoreService = game:GetService("DataStoreService")
local ExampleToggleName = DataStoreService:GetDataStore("ExampleToggleName") -- Change the name to whatever you want

Part.Touched:Connect(function(Hit)
    local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local success, toggleValue = pcall(function()
            return ExampleToggleName:GetAsync(Player.UserId)
        end)

        if success and not toggleValue then
            ExampleToggleName:SetAsync(Player.UserId, true)
            Events.FindExitEvent:FireAllClients()
        end
    end
end)
#

here

tribal burrow
warm vault
#

For here you could add a debounce letme see what i can figure

#

bro you don’t need a data store for a debounce lol

#

Just use a basic debounce

warm vault
#

i meant a basic debounce

#

Ye idk why u would have a data store for a debounce at all

#

i dont?

#

Unless ur trying to save data unless the player goes offline

#

He wants it to be a one time use

tribal burrow
#

yea

warm vault
#

oh

tribal burrow
#

like you use it once and it never happens again

warm vault
#

I thought ur trying to set the data store to be the debounce timer

#

I was like wtf