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

Earning money on Contact to a Block?

First off, sorry if there is a way to put the code in a box. I used lines to show where the code starts and ends. I am currently making an obby and I want the person to get money when they step on a checkpoint. I've been trying to figure out the right code but its not working can someone help me. Thanks in Advance

My first code is some code on a script in the ServerScriptService


game.Players.PlayerAdded:connect(function(p)
	local touch = Instance.new("IntValue")
	touch.Name = "touch"
	touch.Parent = p
	local level = Instance.new("IntValue")
	level.Name = "levelup"
	level.Value = 1
	level.Parent = touch
end)

My Second code is on the bottom. The other stuff is checkpoint code made by Roblox.


local spawn = script.Parent
spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = game.ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Model", game.ServerStorage)
			checkpointData.Name = "CheckpointData"
		end
		local checkpoint = checkpointData:FindFirstChild(tostring(player.userId))
		if not checkpoint then
			checkpoint = Instance.new("ObjectValue", checkpointData)
			checkpoint.Name = tostring(player.userId)
			player.CharacterAdded:connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = game.ServerStorage.CheckpointData[tostring(player.userId)].Value.CFrame + Vector3.new(0, 4, 0)
			end)
		end
		checkpoint.Value = spawn
	end
	spawn.Touched:connect(function(hit2)     --This code right here and down
		if hit2 and hit2.Parent and hit2.Parent:FindFirstChild("Humanoid") then
			if ("levelup") == 1 then
				('levelup').Value = ('levelup').Value + 1
				("ObbyBucks<|>").Value = ("ObbyBucks<|>").Value + 10 
			end
		end
	end)
end)

2 comments

0

code blocks {PLEASE}

0

and also, why did you paste the saving data code and not the touch brick code . . .. . .. . misleading title?

1 answer

Don't use spawn as a variable name, it's used internally for creating new threads.

local myspawn = script.Parent
local serverStorage = game:GetService('ServerStorage')

Create a new function to handle your awards

`local function award(player, level, bucks)
	local stats = player:findFirstChild('touch')
	--[[ You didn't create a bucks stat in your example, so you
	 can wire that up later --]]
	local levelStat = stats and stats:findFirstChild('levelup')	
	if levelStat then 
		levelStat.Value = levelStat.Value + level
	end
end

You don't need two Touched Events. You also can't call an object from thin air like you were trying to do with ("StatName").Value

myspawn.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:findFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = serverStorage:findFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Model", serverStorage)
			checkpointData.Name = "CheckpointData"
		end
		local checkpoint = checkpointData:findFirstChild(tostring(player.userId))
		if not checkpoint then
			checkpoint = Instance.new("ObjectValue", checkpointData)
			checkpoint.Name = tostring(player.userId)
			player.CharacterAdded:connect(function(character)
				-- I wouldn't use this, it can be handled by one script somewhere else.
				wait()
				local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
				HumanoidRootPart.CFrame = serverStorage[tostring(player.userId)].Value.CFrame 
				+ Vector3.new(0, 4, 0)
			end)
		end
		checkpoint.Value = myspawn 
		award(player, 1, 10)
		-- call function
	end	
end)

5 comments

0

it would be better to explain outside of the script rather than crowding up the script with comments

0

You are correct. I will re-edit.

0

I think its nice, but okay lol

0

Where you said,'You also can't call an object from thin air like you were trying to do with ("StatName").Value'. If you were talking about the "ObbyBucks<|>", thats the name of the currency and it is made by another script. It didn't pull it out of thin air.

0

You're essentially trying to compare a string with a number and saying Value is a part of the string. It's like saying "If Hello == the number 9, then Hello.Value += 1".

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