Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my custom character model cause lag when interacting with this?

Asked by 7 years ago

So, I am trying to get sound in different areas in my game. The way I did this was to make a part, insert two scripts and put this in the first one:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        game.Players[hit.Parent.Name].PlayerGui.Sound:Resume()
    end
end)

And this in the second one:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        game.Players[hit.Parent.Name].PlayerGui.Sound:Resume()
    end
end)

After doing that, I pressed play, ran into the area, everything worked. I was happy with that until I loaded into a custom character that I had made, and when I used that to run into the area or part, it played the music as normal but caused tremendous lag. I couldn't hardly move, so I thought maybe the script was running a gazillion times and causing lag, so I added debounce to it. This is what the first script looks like after adding debounce:

local Touched = false

script.Parent.Touched:connect(function(hit)
    if not Touched then
        Touched = true
    if hit.Parent:FindFirstChild("Humanoid") then
        game.Players[hit.Parent.Name].PlayerGui.Sound:Play()
        end 
    end
end)

I added debounce to both of them, although I don't see why it needs it at all. Loaded up my normal character, everything worked fine. Loaded with my custom character, horrible lag same as before. I don't understand what could be causing this, as the script shouldn't cause any lag at all.

Any advice would be greatly appreciated.

1 answer

Log in to vote
0
Answered by 7 years ago

That's not the proper use of a debounce. Since the two if statements aren't connected, you are basically running the same laggy script, except that you are changing a local variable at the start of it.

My suggestion is to just check if the sound is playing through a property called "IsPlaying"

--- An Example
if Sound.IsPlaying == false then
    Sound:Play()
end

Hopefully this fixes your problem!

0
I am a little bit confused as to how to implement this into my script. Would I do if game.Players[hit.Parent.Name].PlayerGui.Sound.IsPlaying == false then Cavedudemann 2 — 7y
0
Yea, that seems right. PreciseLogic 271 — 7y
0
Well, I tried every single way I could think of of adding it to my script, and it still lags. I must be doing something wrong, just not sure what. Some advice on implementing this into my script would be wonderful. I'm still a relatively new scripter, so not too great at implementation of things yet. Cavedudemann 2 — 7y
Ad

Answer this question