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

This script works in Play Solo and Run, but not in an actual game, how can I fix this?

Asked by
jm34 7
8 years ago
function onTouch(hit)
if hit.Name == "MoneyBrick" then
game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value + 1

hit:Destroy()



end
end

script.Parent.Touched:connect(onTouch)

This script makes it so when a "MoneyBrick" goes over the block the script is in it deletes it and then give you 1 money.

1 answer

Log in to vote
0
Answered by
thePyxi 179
8 years ago

Having a 'LocalPlayer' inside a server script will not work among in-game play. The script below is a modified version, which checks to see the player for touching the brick and checks the name. The script below is supposed to be put into the brick and in a server script, not a local script.

function onTouch(hit) --Start the function

    local check = hit.Parent:FindFirstChild("Humanoid") --Find the human that touched the button

    if check ~= nil and script.Parent.Name == "MoneyBrick" then --If a human is found and the name of the brick is "MoneyBrick" , then

        local user = game.Players:GetPlayerFromCharacter(hit.Parent) --get player from touching human
        local stats = user:FindFirstChild("leaderstats") --Find moneyholder

        if stats ~= nil then --If moneyholder exists then

            local money = stats:FindFirstChild("Money") --Get money

            money.Value  = money.Value +1 --amount of money added to the value

            script.Parent:Destroy() --destroy the brick
        end
    else
        print("Not named correctly.") --If the name is not correct, it will just print this.
    end
end

script.Parent.Touched:connect(onTouch) --Calls the function
0
So I replaced this script with the old one and in Output it just said "Not named correctly." repeatedly even though the bricks are called MoneyBrick jm34 7 — 8y
0
what do I do jm34 7 — 8y
0
Can you send a screenshot link of what you are specifically trying to do? thePyxi 179 — 8y
0
Ok I made a picture explaining the problem: https://drive.google.com/file/d/0B5Sa_dRUhnR9ZV9JeFBqaG15alU/view?usp=sharing jm34 7 — 8y
Ad

Answer this question