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

Script doesn't work with table, works with single value?

instead of writing the code out all again what could i do?

local enabled = true
local ding = game.Workspace.Sounds.Ding				-- sound
local part = {game.Workspace.Configuration.Koala1, game.Workspace.Configuration.Koala2, game.Workspace.Configuration.Koala3}	-- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local gui = player:WaitForChild("PlayerGui")
local toolup = gui:WaitForChild("clickE")
local maxpickup = 20
local KoalaGUI = player.PlayerGui.KoalaAdd


	local pp = player.leaderstats.Credits
	UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	
		local item = mouse.Target
		local distance = player:DistanceFromCharacter(item.Position)
if mouse.Target == part and enabled == true and distance <= maxpickup and input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonB then
		ding:Play()
		local newkoala = KoalaGUI:Clone()
		newkoala.Parent = game.Players.LocalPlayer.PlayerGui
		newkoala.Enabled = true
		part.Position = Vector3.new(100,100,100)
		enabled = false
		
		part.Transparency = 1
		part.CanCollide = false
	
		game.ReplicatedStorage.KoalaEvent:FireServer()
		
wait(5)
		newkoala:Destroy()
		enabled = true
		part.Transparency = 0
		part.CanCollide = true
		
	
end
	end)

1 answer

Hello. You have to use an in pairs loop. It loops through a table. Try this:

local enabled = true
local ding = game.Workspace.Sounds.Ding             -- sound
local parts = {game.Workspace.Configuration.Koala1, game.Workspace.Configuration.Koala2, game.Workspace.Configuration.Koala3}    -- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local gui = player:WaitForChild("PlayerGui")
local toolup = gui:WaitForChild("clickE")
local maxpickup = 20
local KoalaGUI = player.PlayerGui.KoalaAdd


    local pp = player.leaderstats.Credits
    UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)

        local item = mouse.Target
        local distance = player:DistanceFromCharacter(item.Position)
for i, part in pairs(parts) do
	if mouse.Target == part and enabled == true and distance <= maxpickup and input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonB then
	        ding:Play()
	        local newkoala = KoalaGUI:Clone()
	        newkoala.Parent = game.Players.LocalPlayer.PlayerGui
	        newkoala.Enabled = true
	        part.Position = Vector3.new(100,100,100)
	        enabled = false
	
	        part.Transparency = 1
	        part.CanCollide = false
	
	        game.ReplicatedStorage.KoalaEvent:FireServer()
	
	wait(5)
	        newkoala:Destroy()
	        enabled = true
	        part.Transparency = 0
	        part.CanCollide = true
	
	
	end
end
    end)

Please accept and upvote this answer if it helps.

2 comments

0

The script does work, thank you , but it doesn't let me pick up another Koala for 5 seconds. I know its because I have a wait in there but.

0

No problem. Please accept this answer.

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