#I have this script the idea is that it only works when the remote event is fired

33 messages · Page 1 of 1 (latest)

amber leaf
#

this script was only supposed to do its job once the remote event is fired and i got it to do that but now when i try to refire the remote event it dosent want do the thing in the script aka destroy the parent model

amber leaf
#

just checked it dosent even do anything i want to

fading flare
#

The :Once connection method disconnects the function from the event when it fires once. If you want it to refire infinity times use :Connect

amber leaf
fading flare
#

This is because you connected the touched event to the function with :Connect, so it will run whenever the remote runs or not. When you connect an event to a function it is stored and will not disconnect until you use the :Disconnect method.

So in this case you should use :Once also when connecting the touched event to the onTouch function.

amber leaf
#

oh thank you let me check that rq

amber leaf
# fading flare Oh

well i accidently broke a lot of the script and had to revert back to the originals script but i figuried it wouldnt matter sense your advice was still the same and it did not work "local Players = game:GetService("Players")
local model = script.Parent -- Assuming the script is placed in the model
local RS = game.ReplicatedStorage
local cand = RS.Cand

-- Assuming the model has a PrimaryPart set
local primaryPart = model.PrimaryPart
if not primaryPart then
error("Model does not have a PrimaryPart set.")
end

cand.OnServerEvent:Connect(function()
local function onTouch(other)
print("Model touched!")

    local character = other.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if humanoid then
        local player = Players:GetPlayerFromCharacter(character)
        if player then
            local tool = player.Character:FindFirstChildOfClass("Tool")
            if tool and tool.Name == "sword" then
                print("Key found! Destroying model.")
                model:Destroy()
            end
        end
    end
end
-- Connect the Touched event for the primary part
primaryPart.Touched:Once(onTouch)

end)
"

fading flare
#

So it will connect the :Once event every time the remote runs so its the same result as :Connect

#

If you put the hasLogicExecuted at the top it will work with :Connect anyways but its not necessary

#

Just put :Once on both connections

#

If it doesnt works then I have no idea, you should check some error?

#

Theres nothing wrong with it

#

If you put once on both

amber leaf
#

yes i put once in both parts I made edites to the script to check which parts worked and which ones didnt and i noticed that player does not print so it just stops right after that for some reason"cand.OnServerEvent:Once(function()
local function onTouch(other)
print("Model touched!")
local character = other.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
print("humanoid")
if humanoid then
local player = Players:GetPlayerFromCharacter(character)
if player then
print("player")
local tool = player.Character:FindFirstChildOfClass("Tool")
if tool and tool.Name == "sword" then
print("Key found! Destroying model.")
model:Destroy()
end
end
end
end
-- Connect the Touched event for the primary part
primaryPart.Touched:Once(onTouch)
end)
"

#

I did an experiment and it does not work once the yhe last ontouch is set to once

#

it only works with connect

fading flare
#

It prints "model touched"?

#

If it does then :FindFirstChildOfClass returned nil

amber leaf
#

yes it does

fading flare
#

Then it connected

amber leaf
#

I changed it to nill it didnt fix it

fading flare
#

Wait

amber leaf
#

no i think the touched part is the tool im using to fire the remote action

fading flare
#

Nvm, you just need to make sure the touched part is what you want

#

Thats the issue

amber leaf
#

ok how do i check for the tool?

fading flare
#

You check if other:IsA("Tool") then

#

But in this case it will get the tools part

#

Not the tool

#

And idk if its a mesh or a part

#

Welp