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

I need a brick that gives people a stat once, for instance, an obby?

Code i'm using:

function OnTouched(Part)
    local player = game.Players:GetPlayerFromCharacter(Part.Parent)
    if player then
        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            leaderstats.Stage.Value = leaderstats.Stage.Value + 1
            else if LocalPlayer.leaderstats.Stage.Value == 1 then
	return
	        

            


        end
    end
end

script.Parent.Touched:Connect(OnTouched)

I'm trying to get it to give me a stat ONCE, but it keeps giving me multiple stats: (Like, i touch it and it gives me like 26 stages.)

1 comment

0

I reckon you should use "elseif" instead of "else if".

4 answers

Try using debounce as a cool down. Here's an example.

local Part=Instance.new('Part',workspace);
local deb=true;
Part.Touched:Connect(function()
	if not deb then return;end;
	deb = false;
	print'Cheez burger';
	deb = true;
end);

Im not very confident in my scripting capabilities, but I think its because after you check for leaderstats on line 6 you add 1 to the leaderstats. there should be an if statement on line 6 pushing line 6 to line 7 and so on, that checks weather or not the value is equal to 1.

Something like

if leaderstats.Stage.Value ~= 1 then
	leaderstats.Stage.Value = leaderstats.Stage.Value + 1
end

And that would replace lines 6 through 8, and possibly all of the extra space.

Hope I helped :D

Make a table, and look for their name in it like with this damage script.

PlayersHit = {}

function damage(hit)
	local h = hit.Parent:findFirstChild("Humanoid")
	if h~= nil then
		for i,tableValue in pairs(PlayersHit) do
			if tableValue == hit.Parent.Name then return end
		end
		table.insert(PlayersHit, hit.Parent.Name)
		h:TakeDamage(25)
	end
end

script.Parent.Touched:connect(damage)

You should not use a LocalScript for this. Here is a better version (server script):

local Players = game:GetService("Players")
local Part = script.Parent

Part.Touched:Connect(function(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Leaderstats = Player:FindFirstChild("leaderstats")
		if Leaderstats then
			if Leaderstats.Stage.Value == Part.Name - 1 then
				Leaderstats.Stage.Value = Part.Name
			end	
		end
	end
end)

--[[
	This will check if your stage is -1 before giving you its current stage value.
	Simply rename your part's name to the value they should get. Ex: 1.
--]]

Note: The reason you shouldn't use a LocalScript is simply because it will not replicate to the server or other clients. Once changed, only you will see the change on your leaderstats.

You should also never trust the client, meaning all checks being done should be handled by the server.

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