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

How do you make a script go off after touching an object?

Asked by 3 years ago

So I wanted to make a thing where after reaching a certain spot new objects will appear, I manage to do one bit but it only does it when you join the game, and I would like it to appear after touching a certain part not when you join the game.

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

If its 1 part then this will be the code:

Local trigger = [Locate your trigger here]
trigger.Touched:Connect(function(h) -- This will start the function whenever its touched!
if game.Players:GetPlayerFromCharacter(h.Parent) then -- This will make sure its actually a player thats triggered it, rather then something else.
-- Your code here
end
end

If you require to run any code on the character, use h.Parent - This should return the character. If its a model, create a sort of hit box on the floor. Put that as the trigger.

0
Should probably mention debouncing as well so it doesn't trigger the event multiple times due to animations/slight clipping. Vingam_Securis 213 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

I recommend using .hit like

script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild ('Humanoid') then if hit.Parent:FindFirstChild ('Humanoid').Health ~= 0 then local player = game.Players:GetPlayerFromCharacter(hit.Parent) game.ReplicatedStorage.Remotes.OpenPetShop:FireClient(player) end end end)

is what I used

Answer this question