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

Why does my script enable the script when it has a certain xp not working?

This is my Script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local GreenPlasma = game.Lighting.GreenPlasma
local copy = GreenPlasma:Clone()
local toggle = false
local pcoins = player:WaitForChild("leaderstats"):WaitForChild("Xp")

mouse.KeyDown:connect(function(key)
	script.Disabled = true
	if pcoins.Value <= 320 then
		script.Disabled = false
	end
	if not toggle and key == "y" then
		
		player.Character.Humanoid.WalkSpeed = 0
		
		local animation = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://01507595343"
		local AtcPlay = player.Character.Humanoid:LoadAnimation(animation)
		AtcPlay:Play()
		
		wait(1)
		
		player.Character.Humanoid.WalkSpeed = 24
		copy.Parent = game.Players.LocalPlayer.Character
		toggle = true
		
		wait(5)
		
	elseif toggle == true and key == "y" then
		player.Character.Humanoid.WalkSpeed = 16
		
		copy.Parent = game.Lighting
		toggle = false
	end
end)

The script from line 9 to 11 is to activate the script only when the player was with xp 320 but it is not working

1 answer

✓ Accepted answer

You shouldn't disable the script. There's no reason to. Use your if statement to determine if code runs instead.

There are a couple more problems with your setup.. i'll list them off.

  • KeyDown is deprecated, and should not be used for new work. Use the UserInputService.InputBegan event.

  • You're cloning GreenPlasma outside of the event, so it will only occur once. Clone inside of the event.

  • The animation object should be created outside of the event, so it is not being created everytime the event fires.

  • Lighting is not meant for storage. Use ReplicatedStorage.

  • And finally, connect is deprecated. Use Connect.

local player = game.Players.LocalPlayer
local pcoins = player:WaitForChild("leaderstats"):WaitForChild("Xp")
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local GreenPlasma = game.ReplicatedStorage.GreenPlasma --MOVE THE OBJECT
local uis = game:GetService("UserInputService") 
local toggle = false
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://01507595343"
local c;


uis.InputBegan:Connect(function(i,p)
	--second parameter detects whether you're typing in a textbox
	if not p then
		--Check input
		if i.KeyCode == Enum.KeyCode.Y then
			--Check coins
			if pcoins.Value < 320 then return end
			--toggle the toggle
			toggle = not toggle
			--check the toggle
			if toggle then
				hum.WalkSpeed = 0
				local AtcPlay = hum:LoadAnimation(animation)
				AtcPlay:Play()
				wait(1)
				hum.Walkspeed = 24;
				--CLONE INSIDE EVENT
				local copy = GreenPlasma:Clone()
				copy.Parent = char;
				--assign to variable outside event since you use object in
				--multiple scopes
				c = copy;
			else
				if c then
					c:Destroy();
				end
				hum.WalkSpeed = 16
			end
		end
	end
end)

NOTE: *You aren't changing where the copy of GreenPlasma is located.. I suspect you want to do so.

4 comments

0

vlw por me informar

0

traduze for Brazilian

0

Its work but The GreenPlasma is invicible

0

Im deleted hum and paste player.Character.Humanoid and this work

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