I have a script that fires a remote event, which parents another script to a player based on which key they've pressed. If they press either 1, 2, or 3, they will be given the corresponding cursor. However, you can press 1, 2, or 3 more than once to swap between cursors. This part is where my issue arises, as it turns out, once a script is parented to the player, it doesn't become disabled once destroyed to make way for a new cursor. From what I've gathered from Googling and looking through forum posts, this seems to be because there's still something referencing the script even after it's been destroyed, so it isn't cleared from memory and stays active. I want to know how to manually disable a script once it's been destroyed.
#Trying to manually disable/turn off a script once it's been destroyed
1 messages · Page 1 of 1 (latest)
This is probably the only relevant code for this since it's the one that does the adding and destroying of scripts to the player.
local userInput = game:GetService("UserInputService")
--VARIABLES/REFERENCES:
local directionBasedCursor_RE = game.ReplicatedStorage.Cursors:WaitForChild("giveDirectionBasedCursor")
local directionBasedCursor_S = game.ReplicatedStorage.Cursors:WaitForChild("DirectionBasedCursor")
local camBasedCursor_RE = game.ReplicatedStorage.Cursors:WaitForChild("giveCamBasedCursor")
local camBasedCursor_S = game.ReplicatedStorage.Cursors:WaitForChild("CamBasedCursor")
local mouseBasedCursor_RE = game.ReplicatedStorage.Cursors:WaitForChild("giveMouseBasedCursor")
local resetCursor_RE = game.ReplicatedStorage.Cursors:WaitForChild("resetCursor")
local player = game.Players.LocalPlayer
--FUNCTIONS:
--FUNC. 1: SEND A REQUEST FOR SPECIFIC TYPE OF CURSOR
userInput.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.One then
camBasedCursor_RE:FireServer(player)
elseif input.KeyCode == Enum.KeyCode.Two then
directionBasedCursor_RE:FireServer(player)
elseif input.KeyCode == Enum.KeyCode.Three then
mouseBasedCursor_RE:FireServer(player)
end
end)
--FUNC. 2: RESET THE PLAYER'S CURRENT CURSOR UPON A NEW REQUEST BEING SENT
resetCursor_RE.OnClientEvent:Connect(function(player)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local workspace = game.Workspace
for i,v in pairs(workspace:GetDescendants()) do
if v.Name == player.Name .. "'s Cursor" and v:IsA("Folder") then
v:Destroy()
end
end
end)```
Basically, every time either 1, 2, or 3 is pressed, anything that's got the player's name and is a folder is deleted and replaced w a new one
it's properly being destroyed since only one can only ever be present at a time in workspace, but this doesn't stop previously deleted scripts from still being active...
add a boolvalue in the player not character and when u wanna destroy it turn the value on and in the script u wanna turn off at the very start say “if boolvalue.Value == true then script:die()” like that
would i add the bool thru an attribute or tag
or do i use a script
wait imma try usin an attribute first...
nevermind, turns out i had just misnamed the script i was looking for when doing if v.Name ==
should have been
if v.Name == "Cursor then
v:Destroy()
end
u could do either really
I do boolvalues cus theyre more beginner friendly
i figured out how to add attributes to player dw