I want to create a script that will increase an individual player’s “money” count every time a part is collected, so how would I go about making this?
I think that I should go along the lines of accessing the Player, initiating values, and then adding +1 to the value when the part is collected. How I would I code this though?
You would use the .Touched() event and increase the player’s money using += and you would probably want to delete the part too using :Destroy().
The script should look something like this if I understood you correctly: (sorry about the comments)
-- this script assumes the money is inside the player's leaderstats.
script.Parent.Touched:Connect(function(hit) -- "hit" will be set to whatever touches the part.
local hum = hit.Parent:FindFirstChild("Humanoid") -- could also use :FindFirstChildOfClass().
if hum then -- making sure that whatever touched the part is a character.
game.Players:GetPlayerFromCharacter(hit.Parent).leaderstats.Money.Value += 1
script.Parent:Destroy() -- self explanatory
end -- not using :GetService("Players") because its just slower.
end)
Let me know if you have any questions or if I misunderstood your question.
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") ~= nil and game:GetService("Players"):GetPlayerFromCharacter(part.Parent) ~= nil then
local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
local value = --put here value you want, example: player:FindFirstChild("leaderstats"):FindFirstChild("Cash").Value
value = value +1
script.Parent:Destroy()
end)
Also if you want cash in leaderstats here is another script put it into ServerScriptService:
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
end)
This can be created only using one script it’s pretty simple :
-- Put this on SSS or in other word ServerScriptService
game:GetService("Players").PlayerAdded:Connect(function(Player) -- Get player
-- Make Leaderstats and Cash or whatever the name is
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Cash = Instance.new("NumberValue", Leaderstats) -- prefer using NumberValue than IntValue because the number doesn't have decimal
Cash.Value = 0
Cash.Name = "Cash"
Player.CharacterAdded:Connect(function(Character) -- Get character
Character.HumanoidRootPart.Touched:Connect(function(touchedPart) -- Detect if the HumanoidRootPart is touching something
if touchedPart:IsA("BasePart") then
-- Increase the Cash's value by 1
Cash.Value += 1
touchedPart:Destroy()
end
end)
end)
end)