#help with part coin collector
1 messages · Page 1 of 1 (latest)
-- Config
local DESPAWN_TIME = 8
local PIECES = 3
local PER_PIECE_REWARD = 5/3 -- each split piece pays this
-- Safe adder that supports fractional rewards even with IntValue Coins
local function addCoins(player, amount)
if not player then return end
local ls = player:FindFirstChild("leaderstats")
if not ls then return end
local coins = ls:FindFirstChild("Coins")
if not coins then return end
if coins:IsA("NumberValue") then
coins.Value += amount
return
end
-- IntValue path: accumulate fractional remainder on the Player
local remainderAttr = "CoinRemainder"
local rem = player:GetAttribute(remainderAttr) or 0
rem += amount
local whole = math.floor(rem)
if whole > 0 then
coins.Value += whole
rem -= whole
end
player:SetAttribute(remainderAttr, rem)
end
-- Give by player name (kept from your original)
local function giveCoinsByName(playerName, amount)
local player = game.Players:FindFirstChild(playerName)
addCoins(player, amount)
end
game.Workspace.ChildAdded:Connect(function(child)
if child.Name ~= "DropPart" then return end
task.wait(0.1)
child.Touched:Connect(function(hit)
if hit.Name ~= "Blade" then return end
local drop = child
if not drop:IsA("BasePart") then return end
if drop:GetAttribute("Processed") then return end
drop:SetAttribute("Processed", true)
-- Who gets paid?
local ownerAttr = drop:GetAttribute("Owner")
local ownerName = typeof(ownerAttr) == "Instance" and ownerAttr:IsA("Player") and ownerAttr.Name
or (type(ownerAttr) == "string" and ownerAttr or nil)
-- Split into pieces and pay per piece
for i = 1, PIECES do
local mini = Instance.new("Part")
mini.Size = drop.Size / 2
mini.CFrame = drop.CFrame * CFrame.new(math.random(-1,1) * 0.5, 0.5,
math.random(-1,1) * 0.5)
mini.Color = drop.Color
mini.Material = drop.Material
mini.Anchored = false
mini.CanCollide = true
mini.Name = "SplitPiece"
mini.Parent = workspace
if ownerName then
giveCoinsByName(ownerName, PER_PIECE_REWARD)
end
task.delay(DESPAWN_TIME, function()
if mini and mini.Parent then mini:Destroy() end
end)
end
-- remove original
if drop and drop.Parent then drop:Destroy() end
end)
end)
pplease help
alr so what's troubling you?
when i step on the part it dosnt give me coins at all
.
i think it might be this: giveCoinsByName(ownerName, PER_PIECE_REWARD)
'cause of the ownerName
but idk
maybe add some debugging to that part and see what it prints and what it doesn't
because this line: local ownerAttr = drop:GetAttribute("Owner") might return nil and local ownerName = ... or (type(ownerAttr) == "string" and ownerAttr or nil) also becomes nil
but idk check it out maybe i'm wrong
other than that i have 0 clue
haven't worked much with attributes tbh
can you help em with something else?
ye?
so bassically i made a instance.new so that it creates a string value called POSV and also something so if a player has leaderstats named "Coins" (100) if they step on a part it takes those 100 coins away now can you help me make ity so when it takes that players coins away instead of when they click the gui giving them 1 coin it gives them 5? this is a diff game btw
mb
np
so you want me to make it so that whenever a Player steps on a part if they have 100 coins, the script removes all 100 Coins AND it changes something so that the GUI button now gives 5 coins per click instead of 1
right?
yes
well i gtg to sleep now but tthis is kind of what you're going for:
touch part script:
local REQUIRED_COINS = 100
local NEW_REWARD = 5
local part = script.Parent -- the part they step on
part.Touched:Connect(function(hit)
-- all the system
end)
gui script:
local player = game.Players.LocalPlayer
local function getReward()
return player:GetAttribute("ClickReward") or 1
end
button.MouseButton1Click:Connect(function()
local amount = getReward()
-- tell server to add coins
RemoteEvent:FireServer(amount)
end)
and the server side just do this: RemoteEvent.OnServerEvent:Connect(function(player, amount)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
local coins = leaderstats:FindFirstChild("Coins")
if not coins then return end
coins.Value += amount
end)
i gtg to sleep
but this is kind of what you're going for