Hello! I have encountered a problem with my code. What I am trying to achieve is to not allow the script to detect the player clicking the part if the part has already been clicked before. This is my first time working with server events so I am quite confused on how to get this to work.
As you can see from above, once I click the part after already clicking it once it still runs the code that I have set in the function that fires once the part is clicked.
This is all of my code that I have running inside of the LocalScript that is placed inside of the tool:
local tool = script.Parent
local equipped = false
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local spleefEvent = tool:WaitForChild("SpleefEvent")
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
mouse.Button1Down:Connect(function()
if equipped then
print(mouse.Target.Name)
if mouse.Target.Name == "spleefGround" then
local target = mouse.Target
--fire event with what ground target was hit
spleefEvent:FireServer(target)
print(target.Name)
end
end
end)
This is all the code that is placed inside of the Server script inside of the tool:
local tool = script.Parent
local spleefEvent = Instance.new("RemoteEvent")
spleefEvent.Parent = tool
spleefEvent.Name = "SpleefEvent"
local spleefEventConnection = nil
function destroyGround(player, target)
for i = 1, 10 do
target.Transparency = i/10
wait(.1)
if target.Transparency == 1 then
target.CanCollide = false
end
end
end
function onEquip()
spleefEventConnection = spleefEvent.OnServerEvent:Connect(destroyGround)
end
function onUnequip()
spleefEventConnection:Disconnect()
end
tool.Equipped:Connect(onEquip)
tool.Unequipped:Connect(onUnequip)
I have tried putting debounces themselves inside of the for loop but for a certain reason it still iterates through the for loop like the debounce does not exist. I have been trying at this for quite some time and I feel like the answer is obvious. Any help is appreciated!
I’m confused as I don’t see any debounces in here (at least not where they should be). Also, it’s bad practice to have server scripts inside of client controlled items. I assumed it wouldn’t work at all in this scenario.
To fix this, you should insert the target into a table. If the table contains the target, it can’t be clicked again.
This seems to happen because, in the destroyGround function, you don’t check if the part was already destroyed.
At the start of the destroyGround function, try adding the following:
function destroyGround(target, player)
if target.Transparrency == 1 then
return
end
This will ensure that parts already destroyed are not affected again.
This simply checks if the part is fully transparent (as your system makes parts fully transparent when they are destroyed). If it is, we can assume that it was already destroyed and ignore the function call by simply returning.
EDIT: My original solution doesn’t account for the fact that the part will not be fully transparent while being tweened to transparent. Try checking if the part has lost any transparency instead. I.E, check if transparency is less than 1.
Your life would be a lot easier if you just create a class for the spleef ground objects. Simply checking the transparency will allow the event to be fired again if it hasn’t completely disappeared. Either setup a cutoff transparency or make a module with a isActive function.
While CollectionService isn’t the same as a pseudoclass, definitely a good point-out. Creating a pseudoclass for minuscule operations isn’t entirely necessary, though grouping objects together with a single identifying string is certainly nice.
You could also iterate through a folder or similar if hierarchy is important to you and you have a small area to work with.