Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My "Cash on Kill" script is only repeating itself 2 times and then not working at all?

local Human = script.Parent:WaitForChild"Humanoid"
local Player = game.Players:GetPlayerFromCharacter(script.Parent)
Human.Died:Connect(function()
	if Player then
		local Krak = Player:FindFirstChild"leaderstats":FindFirstChild"Krak" 
		if Krak then
			Krak.Value = Krak.Value - 20 
		end
	end
	local Creator = Human:FindFirstChild"creator"
	if Creator and Creator.Value and 
		Creator.Value:FindFirstChild"leaderstats" and 
		Creator.Value.leaderstats:FindFirstChild"Krak" then 
		Creator.Value.leaderstats["Krak"].Value = Creator.Value.leaderstats["Krak"].Value + 25 
	end
end)

I have this script here, it's supposed to go inside of Zombie and give the player 25 dollars ("Krak" the name of the currency I'm using for my game) every time the humanoid reaches 0 health. It works for the first two times that the script runs but then it just appears to run but not yield any Krak. There is no output into the console whatsoever. I have tried for days doing research with one of my friends to find any possible issues, we found nothing. I'm hoping its just a simple fix I could get some help with here. Below is also my leaderboard script if that helps any. Thanks in advance!

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Krak = Instance.new("IntValue")
	Krak.Name = "Krak"
	Krak.Value = 10000
	Krak.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)

3 comments

0

WaitForChild and FindFirstChild are functions, you're forgetting the parentheses

0

What is the script parented to?

0

The leaderboard script is in game.serverscriptstorage and the "Cash on Kill" script is parented to the model of the zombie if that answers your question.

1 answer

I would recommend doing it like this, this script goes inside the thing to kill:

local tagHelp = script.Parent

script.Parent:FindFirstChild("Humanoid").Died:Connect(function()
	if tag ~= nil then
		if tag.Value ~= nil then
			local leaderstats = tag.Value:FindFirstChild("leaderstats")
			local krak = leaderstats:FindFirstChild("Krak")
			if krak then
				krak.Value += 20
			end
		end
	end
end)

In your weapon script that deals the damage do this:

local function tagCreator(NPC, player)
		local Creator_Tag = Instance.new("ObjectValue")
		Creator_Tag.Name = "creator"
		Creator_Tag.Value = player
		Creator_Tag.Parent = NPC
		game:GetService("Debris"):AddItem(Creator_Tag, 2)
		Creator_Tag.Parent = game.Workspace:FindFirstChild(NPC.Name)
end

--Un tags the victim if someone has already tagged them
local function unTag(NPC)
	for i, v in pairs(NPC:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

In the weapon script where the weapon checks if the object is a player and damages them add this:

local NPCthing = Humanoid.Parent

unTag(NPCthing)
tagCreator(NPCthing, player)

If you need any help with what I mean feel free to ask :) Also if there are any errors just say.

9 comments

0

Also make sure the scripts are server scripts

0

So... I'm using a weapon system called ACS because I am no where near knowledgeable to make my own gun system yet. Anyways ACS stores everything server side like the main scripts, functions ext. And any messing about with those scripts always seems to mess something up. Would there possibly be a way that I could do everything in the "leaderstats" script or inside of the zombie?

0

Or is there any possible way to just fix the current script with its problem of only running twice?

0

Yeh sure i could see if i can find a way

0

Do you know how the creator tag is being created

0

No I don' think so, I haven' done much dissecting of the main scripts themselves because everything is put to very user friendly configs to edit everything. I can do some digging to look for how its being created. In the mean time...

0

all I can think of is just adding a script:Destroy() at the end of the script so it cant duplicate but apart from that i dont know Im sorry if i was of no help

0

That's okay man, I'll try adding script:Destroy() and see what it does.

This discussion is preserved as it was. New answers, comments, and votes are closed.