#I have this script the idea is that it only works when the remote event is fired
33 messages · Page 1 of 1 (latest)
just checked it dosent even do anything i want to
The :Once connection method disconnects the function from the event when it fires once. If you want it to refire infinity times use :Connect
thats what i wanted. I wanted the script to only work when the remote event was fired and stop after 0.3 seconds but it keeps going right after
Oh
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.
oh thank you let me check that rq
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)
"
Yeah because you put :Connect on the remote event.
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
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
yes it does
Then it connected
I changed it to nill it didnt fix it
Wait
no i think the touched part is the tool im using to fire the remote action
ok how do i check for the tool?