Is there a way to give a hat, if something happens?

I made a hat, but I want to know how to give it to someone, if say, they have a walkspeed over/under 16. How do I achieve that? Please also tell me where to leave the script.

Hope this can help you a little.

Put this in StarterCharacterScripts, make sure it is a normal script.

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Running:Connect(function(Speed)
	if Speed > 16 then
		-- give hat
	else
		-- delete hat, if you dont want this just delete the else
	end
end)

1 Like

Can’t you just do:

if Humanoid.WalkSpeed > 16 then

and then the give hat script one line below. Because you don’t technically need to see if the player is running to run the script. Then again I’m not the best scripter so mine is just an assumption based off what I know.

how do you necessarily give the hat?

No, that would not work unless if you put it in a while loop of some sort which is bad for performance and more complicated than just using an event.

An if statement in this case, would only run once when the game starts. That’s why we would use events.

2 Likes

You’d weld it to the player’s head using WeldConstraints, I recommend adding a debounce though so that that it doesn’t give you the hat more than once when the player’s walkspeed is more than 16.

No, I mean through the script

30

Just like I said before, using Instance.new(“WeldConstraint”) and setting the Part0 and Part1 through the script.

Have you considered using the Accessory class? This may possibly be more efficient than using a Weld.

you don’t understand. i mean that if statements don’t work without something happening. what i mean is, if i jump, and make an if statement, and nothing is inserted inside of the if statement, nothing is going to happen

Yes, it’s an Accessory. I just want to give the hat when the walkspeed is over/under 16

Well, that just changes everything.

If it’s an accessory all you need to do is just put the hat inside of the Character, make sure it’s not anchored though!

so your saying, it’s going just give the hat to everyone when it’s over 16? and your script doesn’t work

If you’d like for it to be created through the server then that is your responsibility to utilize Remote Events in order to do so, however, if you’re interested in a client-side script then I’d suggest something similar to this;

localHumanoid.Running:Connect(function(playerSpeed)

if playerSpeed > 16 then -- If you want it to be equivalent to 16 *or higher*, then consider using >= as an operator.
	
	-- define hat
	
	localHumanoid:AddAccessory(definedHatHere)
	
elseif playerSpeed < 16 then
	
	-- remove hat
	
end
	
end)

Please make sure that you’re actively defining all the variables used above as it is not code which will work natively. It is your responsibility to define and properly remove the hat, but to do something similar, you could look into RemoveAccessories.

Am I getting something wrong here? Can you please tell me the exact specific thing that you want to accomplish. This should work absolutely fine if this is in a normal script, not a localscript inside of StarterCharacterScripts.

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Hat = game:GetService("ServerStorage").Hat -- or wherever your hat is
local hasHat = false -- to prevent the hat from duplicating when it's on the character

Humanoid.Running:Connect(function(Speed)
	if Speed > 16 and not hasHat then
		HatClone = Hat:Clone() -- global so that we can use it in the "else", if you dont want the else, localize this.
		HatClone.Parent = Character
		hasHat = true
	else
		HatClone:Destroy()
		hasHat = false
	end
end)

Where should I put this?

30 ch

It should be a LocalScript inside of “StarterCharacterScripts.”

Parenting the Accessory to the Character will not work; instead, you should use Humanoid:AddAccessory() as shown by @squishyLotus.

It will work if the script is not a local script. Which is why the original script that I made is a normal script, not a local one.

Ok, so the answer to this, is:

   local MaxWalkSpeed = 16
   local DunceCap = game:GetService("ServerStorage").DunceCap
   local IsDunce: {[Player]: boolean} = {}

    local function MakeDunce(player: Player)
    local character = player.Character
    if (character) then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if (humanoid) then
            local accessory = DunceCap:Clone()
            humanoid:AddAccessory(accessory)
        end
    end
    IsDunce[player] = true
    print(("%s is a dunce!"):format(player.Name))
end

local function CheckForDunce(player: Player, humanoid: Humanoid)
    if (humanoid.WalkSpeed > MaxWalkSpeed) then
        MakeDunce(player)
    end
end

game:GetService("Players").PlayerAdded:Connect(function(player: Player)
    player.CharacterAdded:Connect(function(character: Instance)
        if (IsDunce[player]) then
            MakeDunce(player)
        else
            local humanoid = character:FindFirstChildOfClass("Humanoid")
            if (humanoid) then
                CheckForDunce(player, humanoid)
                humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
                    CheckForDunce(player, humanoid)
                end)
            end
        end
    end)
end)

this is a normal script, in ServerScriptService, and if your storing the hat anywhere (ReplicatedStorage or ServerStorage), replace the ServerStorage part of the script to where you are storing it.