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

How can I make my Gamepass script perform this?

So I have a script that Goulestem helped me edit and it adds speed when you join my game if you have a certain Gamepass. It works but it constantly adds the amount I want it to, it doesnt just add it once it keeps going. Like if I want it to add one it will add 1 the whole time you are alive. I only want it to add once everytime you join for the whole game not always adding more. my script:

local gps = game:GetService("GamePassService");
local id = script:WaitForChild("GamePassID");
local Speed = script:WaitForChild("WalkSpeedAmount");

game.Players.PlayerAdded:connect(function(Player)
    Player:WaitForDataReady()
    if gps:PlayerHasPass(Player , id.Value) then
        coroutine.resume(coroutine.create(function()
            while wait() do
                repeat wait() until Player.Character~=nil 
                local hum = Player.Character.Humanoid
                hum.WalkSpeed = hum.WalkSpeed + Speed.Value
            end
        end))
    end
end)

Inside this script is a GamePassID value which represents the ID, and the WalkSpeed which represent the amount of speed I want to add. Please help me if you can. THANKS!

1 answer

✓ Accepted answer

The problem here is that you're constantly adding the WalkSpeed in a Loop. Take the code out of the loop and you're good(:

Also, I don't know why you have a coroutine there(;

local gps = game:GetService("GamePassService");
local id = script:WaitForChild("GamePassID");
local Speed = script:WaitForChild("WalkSpeedAmount");

game.Players.PlayerAdded:connect(function(Player)
	Player:WaitForDataReady()
	if gps:PlayerHasPass(Player , id.Value) then
		repeat wait() until Player.Character~=nil 
		local hum = Player.Character.Humanoid
		hum.WalkSpeed = hum.WalkSpeed + Speed.Value
	end
end)

-EDIT-

To make your character always have the walkspeed enhancement, you need to add the speed in a CharacterAdded Event. So that whenever the player respawns, it will check if they have the pass and then if they do, give them the enhanced speed. Checking with a CharacterAdded event also prevents users of your game having to rejoin to activate the GamePass(:

local gps = game:GetService("GamePassService");
local id = script:WaitForChild("GamePassID");
local Speed = script:WaitForChild("WalkSpeedAmount");

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(char) --CharacterAdded event
		char:WaitForChild("Humanoid") --Just to be safe(;
		if gps:PlayerHasPass(Player , id.Value) then
			local hum = Player.Character.Humanoid
			hum.WalkSpeed = hum.WalkSpeed + Speed.Value
		end
	end)
end)

2 comments

0

One more problem, when I die it goes away I want them to have it the whole game, how can I do that?

0

To keep the speed, even when you respawn, you're going to have to add the speed in a CharacterAdded event. I'll edit one in right now(;

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