Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a script give cash to the owner of a part when that part touches the script?

Asked by 3 years ago
local ting = 0 --debouncer

function onTouched(hit)

    if ting == 0 then --debounce check
    ting = 1 --activate debounce
    check = game.Workspace.Cake.Parent("Player Who Owns the Part").Value
    player = game.Parent.Parent:FindFirstChild("Humanoid")
    cakeowner = hit.check

    if cakeowner ~= nil then --If a cake is found, then
    if player == check then         
        local stats = user:findFirstChild("leaderstats") --Find moneyholder

        if stats ~= nil then --If moneyholder exists then
                local cash = stats:findFirstChild("Cash") --Get money



function touched(hit)
if hit.Name == "Batter" then
Cash.Value = Cash.Value + 5

elseif hit.Name == "Shaped Batter" then
Cash.Value = Cash.Value + 10

elseif hit.Name == "Sugared Batter" then
Cash.Value = Cash.Value + 20

elseif hit.Name == "Unflavoured Cake" then
Cash.Value = Cash.Value + 25

elseif hit.Name == "Heated Cake" then
Cash.Value = Cash.Value + 50

elseif hit.Name == "Cake" then
Cash.Value = Cash.Value + 100




 wait(5) --wait-time before button works again
        end

    end

    ting = 0 --remove debounce
    end

end
        end
    end
script.Parent.Touched:connect(onTouched)

I'm trying to make a script that gives you Cash when a part named Batter (or the others) touches the part that contains the script.

I need the script to read the Batters ObjectValue string, which has a players name in it.

If the players name equals the ObjectValue, then it should give that person Cash.

0
replied to your comment raid6n 2196 — 3y

3 answers

Log in to vote
1
Answered by 3 years ago

I think I understand what you want here, but my script may be incorrect even by myself. If you could please explain more specifically what you're trying to do that would be great.

local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local DebounceHit = false

function OnTouched(Hit)
    local Character = (Hit.Parent:IsA("Model") and Hit.Parent) or Hit:FindFirstAncestorOfClass("Model")
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
    local Player = Players:GetPlayerFromCharacter(Character) or (Hit:IsA("BasePart") and Hit:GetNetworkOwner())
    if DebounceHit == true then
        return
    end
    DebounceHit = true
    local LeaderboardStatistics = Player:FindFirstChild("leaderstats")
    local CashStatistic = LeaderboardStatistics:FindFirstChild("Cash")
    if Hit.Name == ("Batter") then
        CashStatistic.Value = CashStatistic.Value + 5
    elseif Hit.name == ("Shaped Batter") then
        CashStatistic.Value = CashStatistic.Value + 10
    elseif Hit.Name == ("Sugared Batter") then
        CashStatistic.Value = CashStatistic.Value + 20
    elseif Hit.Name == ("Unflavoured Cake") then
        CashStatistic.Value = CashStatistic.Value + 25
    elseif Hit.Name == ("Heated Cake") then
        CashStatistic.Value = CashStatistic.Value + 50
    elseif Hit.Name == ("Cake") then
        CashStatistic.Value = CashStatistic.Value + 100
    end
end

script.Parent.Touched:Connect(OnTouched)
0
Basically what I need is for the Batter to give the owner of the part, money. This will explain it: https://ibb.co/M9zJCLb Nath390Fish 19 — 3y
0
Your script is very close. It gives money to the player. :) I've just gotta change a couple things I think.. As once it gives money, it stops working. Nath390Fish 19 — 3y
0
Hey. I got it to work! :) Thank you. Nath390Fish 19 — 3y
Ad
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Edited code + beautifier:

local ting = 0

function onTouched(hit)
    if ting == 0 then
        ting = 1
        local cakeowner = hit.Parent
        local character = hit.Parent.Parent
        local player = character.Parent

        if cakeowner.Parent ~= character then
            if player then
                local stats = player:FindFirstChild("leaderstats")

                if stats ~= nil then
                    local cash = stats:findFirstChild("Cash")

                    if hit.Name == "Batter" then
                        Cash.Value = Cash.Value + 5
                    elseif hit.Name == "Shaped Batter" then
                        Cash.Value = Cash.Value + 10
                    elseif hit.Name == "Sugared Batter" then
                        Cash.Value = Cash.Value + 20
                    elseif hit.Name == "Unflavoured Cake" then
                        Cash.Value = Cash.Value + 25
                    elseif hit.Name == "Heated Cake" then
                        Cash.Value = Cash.Value + 50
                    elseif hit.Name == "Cake" then
                        Cash.Value = Cash.Value + 100

                        wait(5)
                    end
                end

                ting = 0
            end
        end
    end
end
script.Parent.Touched:connect(onTouched)

0
Haha thanks. Looks so much neater. Nath390Fish 19 — 3y
0
if it works pls accept i need rep raid6n 2196 — 3y
0
Hey. It didn't work, I'll experiment with the codes you've both suggested though.. I'll try explain as best as possible aswell.. Nath390Fish 19 — 3y
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

So what I am trying to do is get the Batter to give the owner money if the Batter touches the Moneygiver part.

Every piece of Batter has a string (ObjectValue) as a child.

The strings name is "Player Who Owns the Part".

Inside the strings value is the name of the person who created the Batter, using a button. (Ideally I'd want a script that changes the value to whoever touches the Batter..)

When the Batter touches the Moneygiver part, I want it to give money to the player with the same name thats inside of the Strings Value. (String.Value = plr)

EDIT:

local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local DebounceHit = false

function OnTouched(Hit)
    local Character = (Hit.Parent:IsA("Part") and Hit.Parent) or Hit:FindFirstAncestorOfClass("Part")
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
    local Player = Players:GetPlayerFromCharacter(Character) or (Hit:IsA("BasePart") and Hit:GetNetworkOwner())
    if DebounceHit == true then
        return
    end
    DebounceHit = true
    local LeaderboardStatistics = Player:FindFirstChild("leaderstats")
    local CashStatistic = LeaderboardStatistics:FindFirstChild("Cash")
    if Hit.Name == ("Batter") then
        CashStatistic.Value = CashStatistic.Value + 5
    elseif Hit.name == ("Shaped Batter") then
        CashStatistic.Value = CashStatistic.Value + 10
    elseif Hit.Name == ("Sugared Batter") then
        CashStatistic.Value = CashStatistic.Value + 20
    elseif Hit.Name == ("Unflavoured Cake") then
        CashStatistic.Value = CashStatistic.Value + 25
    elseif Hit.Name == ("Heated Cake") then
        CashStatistic.Value = CashStatistic.Value + 50
    elseif Hit.Name == ("Cake") then
        CashStatistic.Value = CashStatistic.Value + 100
        end
    wait(5)
  DebounceHit = false
end



script.Parent.Touched:Connect(OnTouched)

moonn_mii edited the original code, then I made some little changes.

I added wait(5) DebounceHit = false to the bottom so that after 5 seconds it lets you get money again.

Then for Hit.Parent:IsA("Model") I changed it to Hit.Parent:IsA("Part") As the Batter is stored inside of a part.

Thank you moonn_mii. :)

Answer this question