I’ve been trying to find out why the transparency of my model is changing very rapidly. I have yet to find a solution to this, as I’ve tried everything I could think of. Getting some help with this would be greatly appreciated. Below, you’ll find the scripts that are causing this sudden transparency shift.
Localscript
local AzureOre = game.Workspace.AzureOre
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0
Hitbox.Touched:Connect(function(player)
local function Check()
if Handle:GetAttributeChangedSignal("Activated") then
else return end
Count += 1
local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
if Cast then
if Cast.Instance.Name ~= AzureOre.Name then return end
else
return
end
end
repeat task.wait(1) Check() until Count == 5
OreTouchedEvent:FireServer(AzureOre)
Count = 0
end)
Tool script
local Tool = script.Parent
local Handle = Tool.Handle
Tool.Activated:Connect(function()
Handle:SetAttribute("Activated")
end)
Tool.Deactivated:Connect(function()
Handle:SetAttribute("Deactivated")
end)
Serverscript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = ReplicatedStorage.OreTouchedEvent
local OreTransparency = 0
local debounce = false
OreTouchedEvent.OnServerEvent:Connect(function(player, Ore)
local char = player.Character
local hum = char:FindFirstChildOfClass("Humanoid")
if hum then
local pickaxe = char:FindFirstChild("Pickaxe")
if pickaxe then
wait(1)
for _, v in ipairs(Ore:GetChildren()) do
if v:IsA("BasePart") and v.Name ~= "Hitbox" then
v.Transparency += 0.25
end
end
end
end
end)
The problem here is that it goes over every part, setting the transparency to + 0.25, and in order to properly function i wish for it to go over every part and change the transparency of all the parts to 0.25 as fast as possible, but only once per touched event.
You should put the above the event and outside of it, not only does it harm readability, but you’re literally creating a whole new function each time the event fires.
wait() is also deprecated, please use task.wait() as it’s the new version and it has more benefits.
you don’t need to use pairs/ipairs anymore, you can simply just write “in”, not only is this also again better for readability, but it’s faster to loop through the table.
The only reason you’d need to use ipairs is if you don’t want to keep looping through if there is a nil object, which I can’t say many times i’ve had to do this.
Ohhhh, this is because when you touch a basepart in roblox. Multiple parts are touching it and it fires very quickly. I suggest you learn about something called “debouncing”
local function Check()
Count += 1
local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
if Cast then
if Cast.Instance.Name ~= AzureOre.Name then return end
else
repeat task.wait(1) Check() until Count == 5
OreTouchedEvent:FireServer(AzureOre)
Count = 0
end
end
Hitbox.Touched:Connect(Check)
.Touched is very inefficient for hitboxes and you’d see that is very apparent in the actual server of your game if you had many players, if you want more of an accurate hitbox. I suggest you use raycasting/magnitude or :GetPartsBoundsInBox(), which are more accurate in their detections and would be better as a hitbox.