My script i made won't work as intended, i don't see anything wrong?

  1. i’m trying to make it to where the players cash will increase upon the death of the zombie basically but update the cash value that is from the server cause i made a datastore for it and i want the stats that the player gained from killing the zombie to save, and it does work but.

  2. the problem is, the script works and everything But it only runs/works once? and then it stops with no error no anything and i can’t figure out what’s wrong? maybe a simple fix i can’t seem to find?

  3. i tried changing it to wait for the remote to trigger and or wait for cash to be there and much more i just don’t know what else to do?

server script is

-- Create a RemoteEvent in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:FindFirstChild("ZombieKilled")
local HowMuchCashToReward = math.random(1, 30)
-- Listen for the RemoteEvent
Remote.OnServerEvent:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local cash = leaderstats:FindFirstChild("Cash")

	if cash and Remote then
		
		cash.Value = cash.Value + HowMuchCashToReward
	end
end)

local script is

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("ZombieKilled")
local zombie = game.Workspace:FindFirstChild("Zombie") -- Wait for the Zombie model to exist
local zombieHumanoid = zombie:WaitForChild("Zombie")
local player = game.Players.LocalPlayer
-- Listen for the zombie's death
zombieHumanoid.Died:Connect(function()
	
	if player then
		print("Zombie Died", player)
		remote:FireServer(player, zombieHumanoid, remote, zombie)
	else
		print("Zombie Died, but no associated player found.")
	end
end)

you only registering the death of the zombie ONCE on that humanoid, once that humanoid dies it wont fire again, simple solution is putting a folder named zombies, then inside parenting zombies to that folder when they spawn, and connect a ChildAdded event and check if the model is a valid zombie model either by checking its name or properties so in case you add something else thats not a zombie it doesnt try to register the died event.

thank you i was thinking that already of it running once but i couldn’t figure out how i could do it more than once so i just tried doing everything else cause i wasn’t even sure i needed it like that cause i couldn’t test it cause i didn’t know how to do it. so thank you for that information i’m going to try it

1 Like

worked! thank you so much for the idea it really helped

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.