Need help with a script

Hi, this is my first time posting on Roblox DevForum. I’m trying to make a Bounty system in which, if a player kills other player it’s bounty will rise and when the killer dies, its bounty will drop as a “Money” model which can be picked up by other players. Also the killer’s bounty will reset. I’ve successfully made the scripts but I’m stuck in a part that every time the killer dies, it also resets the value of the model it dropped, so every time I pick up the money, I get an amount of 0. So I’m trying to know that if there’s any way to store the value of the bounty in the model?

local originlMoney = game.ServerStorage.Money
game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local Money = originlMoney:Clone()			
			Money.Parent = game.Workspace.MoneyDrops
			Money.CFrame = character.HumanoidRootPart.CFrame
			Money.Transparency = 1
			Money["1stDecal"].Transparency = 1
			Money["2ndDecal"].Transparency = 1
			Money.Script.Disabled = true
			wait(7)
			Money["1stDecal"].Transparency = 0
			Money["2ndDecal"].Transparency = 0
			Money.Transparency = 0
			Money.Script.Disabled = false
			-- Bounty reset
			player.PlayerValues.Bounty.Value = 0 -- I'm pretty sure that this line is the problem
		end)
	end)
end)

Here’s the code which I used in ServerScriptService

local Players = game:GetService("Players")

local money = script.Parent

local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		-- Destroy the pickup
		money:Destroy()
		-- Update the player's leaderboard stat
		local player = Players:GetPlayerFromCharacter(partParent)
		local leaderstats = player.leaderstats
		local moneyStat = leaderstats and leaderstats:FindFirstChild("Money")
		local value = player.PlayerValues.Bounty.Value
		if moneyStat then
		moneyStat.Value = moneyStat.Value + value
			

		end
	end
	
end
money.Touched:Connect(onPartTouch)

also here’s the script i used in the model to get the bounty value

Try this

local Players = game:GetService("Players")
local money = script.Parent

local function onPartTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		-- Update the player's leaderboard stat
		local player = Players:GetPlayerFromCharacter(partParent)
		local leaderstats = player.leaderstats
		local moneyStat = leaderstats:FindFirstChild("Money")
		local value = player.PlayerValues.Bounty.Value
		if moneyStat then
		moneyStat.Value = moneyStat.Value + value
           --if you want to change her the bounty, just do:
           --value += *amount here*
		end
	end
end
money.Touched:Connect(onPartTouch)
local originlMoney = game.ReplicatedStorage.Money

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local Money = originlMoney:Clone()			
			Money.Parent = game.Workspace.MoneyDrops
			Money.CFrame = character.HumanoidRootPart.CFrame
			Money.Transparency = 1
			Money["1stDecal"].Transparency = 1
			Money["2ndDecal"].Transparency = 1
			Money.Script.Disabled = true
			task.wait(7)
			Money["1stDecal"].Transparency = 0
			Money["2ndDecal"].Transparency = 0
			Money.Transparency = 0
			Money.Script.Disabled = false
			
			player:WaitForChild("PlayerValues").Bounty.Value = 0 
		end)
	end)
end)
1 Like

money:Destroy(), this line in the first function, will delete that part, which means - it will delete everything inside that part aswell, and as I can tell, that script is under that part. Which means, the rest of the code wouldn’t be working, as it got deleted within the script and the part.

If you want to destroy that part, do it in that function - but after you’ve done the rest of the code, for e.g - after you gave the player ‘moneyStat’.

1 Like

Sorry. I just tried it. It’s not working.

Show us screenshots of where the scripts and everything relevant to this topic are located.

You need to store the value you want the pickup to have somewhere. Right now you are just referencing the player’s new bounty state, not the one they had when they died. You can use attributes to accomplish this.

-- In the first script
Money:SetAttribute("value", Player.PlayerValues.Bounty.Value);

-- In the second script
moneyStat.Value = moneyStat.Value + money:GetAttribute("value")

Also you need to migrate the changing of leaderstats to a server script, the client cannot change them (I’m assuming the second script is a local script) EDIT: Forgot that the part touched can just be done from a script, no need to use remotes here.

2 Likes

If one of those scripts are localScripts, then change them to scripts.

Plus, you can use Attributes, as mentioned above.

1 Like

Thanks man I was trying to solve it for days