GetPlayerFromCharacter failing to execute a second time for a powerup

I’m trying to create a powerup that increases speed. Here’s the script that handles picking up the powerup and respawning it:

local powerup = script.Parent

local db = powerup.db

local plrsService = game:GetService("Players")

for i, v in pairs(powerup:GetChildren()) do
	if v:IsA("BasePart") then
			if db.Value == false then
				db.Value = true
				local plr = plrsService:GetPlayerFromCharacter(hit.Parent)
				plr.powerupEffects.speedSodaTime.Value = 7
				db.Value = false

And here’s the script for counting the time left on the powerup(it’s a LocalScript):

local plr = game.Players.LocalPlayer

local speedSodaTime = plr.powerupEffects.speedSodaTime
local speedSodaStatus = plr.powerupEffects.speedSodaStatus

	if speedSodaStatus.Value == false then
		speedSodaStatus.Value = true
			speedSodaTime.Value = speedSodaTime.Value - 1
		until speedSodaTime.Value <= 0
		speedSodaStatus.Value = false

These scripts work perfectly when I pick up the powerup for the first time, but for some strange reason the GetPlayerFromCharacter part of the first script never executes after the first time.

Everything else still works after the first time. I’ve tried checking that the character model of the player isn’t counted as part of the powerup and simplifying the debounce for the powerup spawning, but I’m still stuck. Can anyone help me? I do need to have a IntValue or NumberValue representing the time left on the powerup, because they’re going to work like the powerups from Adventure Forward 2.

Also, does anyone have any optimizations for these scripts?

When you use GetPlayerFromCharacter on a model thats not a player it will return nil. You should check if the player exists before doing anything with the plr variable. I dont know if this will fix the problem though.

local plr = plrsService:GetPlayerFromCharacter(hit.Parent)

if plr then
    plr.powerupEffects.speedSodaTime.Value = 7
1 Like

It didn’t fix the problem. Basically everything—even the stuff in the code output window—was the same.

You can server-side the entire thing. The rest would involve using remotes. Something to look into is how “global” the debounce is. It applies to all players, all with one shared debounce.

Not sure what but I think the debounce is failing somehow and I have no clue what it is. Otherwise it’s something I overlooked.

Oh, the debounce isn’t failing. The debounce is there to respawn the powerup. There’s no debounce for collecting the powerup. Plus, I’m only going to have one player in the server at all times. The issue is, the value of speedSodaTime isn’t changing after being picked up twice.