Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

4 comments

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

0

what do I do

0

Can you send a screenshot link of what you are specifically trying to do?

0

This discussion is preserved as it was. New answers, comments, and votes are closed.